I am just starting Ruby as my first programming language. I have this
version installed on my Linux: ruby 1.9.3p194
While reading this book, I tried this example:
wordlist.rb:
code_words = {
‘Milkshake’ => ‘See what you mean to me, sweet cakes and milkshakes’,
‘Strawberry’ => ‘People moving all the time, inside a perfectly
straight line, don’t you wanna curve away?’
}
and code.rb has:
require ‘wordlist’
Get the evil idea and swap in code words
print "Enter your new idea: "
idea = gets
code_words.each do |real, code|
idea.gsub!( real, code )
end
Save the jibberish to a new file
print "File encoded, Please enter a name for this idea: "
idea_name = gets.strip
File::open ( “idea-” + idea_name + “.txt”, “w” ) do |f|
f << idea
end
Code_words = {
‘Milkshake’ => ‘See what you mean to me, sweet cakes and milkshakes’,
‘Strawberry’ => ‘People moving all the time, inside a perfectly
straight line, don't you wanna curve away?’
}
code.rb
require ‘/home/arslan/Work/01-June/ruby/wordlist’
Get the evil idea and swap in code words
print "Enter your new idea: "
idea = gets
Code_words.each do |real, code|
idea.gsub!( real, code )
end
Save the jibberish to a new file
print "File encoded, Please enter a name for this idea: "
idea_name = gets.strip
File::open( “idea-” + idea_name + “.txt”, “w” ) do |f|
f << idea
end
1: There should be no space between File::open and (
Well done! That one can be a bit tricky to figure out on your own.
Just to expand on this a bit more: this may seem like a picky
stylistic issue, but apparently it’s a parser problem. (Not quite
sure I’d call it an outright “bug”, since it does seem to be in line
with the Ruby specs.) The space makes the parser think that the next
thing isn’t a parameter list, but a single parameter, as a
paranthesized expression. (Possibly to be followed by a comma and
more parameters.) So it tries to parse it as that, and when it runs
into the comma, it doesn’t know what to do, because a comma-separated
set of expressions is not a valid expression itself in Ruby. See:
The space makes the parser think that the next
thing isn’t a parameter list, but a single parameter, as a
paranthesized expression. (Possibly to be followed by a comma and
more parameters.) So it tries to parse it as that, and when it runs
into the comma, it doesn’t know what to do, because a comma-separated
set of expressions is not a valid expression itself in Ruby.
Thank you Dave! This makes sense now to me.
[…]
for a simpler example. Check out the rest of the slides while you’re
there; they’re from my Ruby Gotchas presentation.
I did… and I’m not sure what to think of these gotchas. I don’t know
whether these are things “need fixing” or whether these are just they
way Ruby is.
Walton H. wrote in post #1113433:
[…]
Just to hopefully clear this one up a bit, The Poignant Guide is
starting to show it’s age a bit. In Ruby 1.8.* require used to search
the current directory for files to include when using require, which is
the behavior the book was relying on.
[…]
In 1.9.x require was changed to no longer search the
current directory, and require_relative (which is what you want to use
here) was introduced to require a file relative the the current one.
Thank you Walton! This helps a lot.
With Why gone, I don’t think anyone is maintaining the Guide, which is
too bad. It’s one of my favorite programming books.
I have been wondering since yesterday which book(s) I should pick for my
journey… a book written by an expert, for beginners, but not written
like a manual.
The Well-Grounded Rubyist by David Black looks interesting (from
reviews). And the Learn to Program by Chris P. also looks good.
Although I’m not sure which one should I start with.
Just to hopefully clear this one up a bit, The Poignant Guide is
With Why gone, I don’t think anyone is maintaining the Guide, which is
too bad. It’s one of my favorite programming books.
I have been wondering since yesterday which book(s) I should pick for my
journey… a book written by an expert, for beginners, but not written
like a manual.
The Well-Grounded Rubyist by David Blank looks interesting (from
reviews). And the Learn to Program by Chris P. also looks good.
Although I’m not sure which one should I start with.
“Learn to Program” is a really nice book, but more addressed to
complete programming newbies. When you have some experience you
can browse through it rather quickly.
I do not know “The Well-Grounded Rubyist”.
When you already have a basic knowledge and understanding then you
might have a look at “Eloquent Ruby” by Russ Olsen. IMO a great book
for getting a feeling for the language.
2: Full file path is needed in the “require” line
Just to hopefully clear this one up a bit, The Poignant Guide is
starting to show it’s age a bit. In Ruby 1.8.* require used to search
the current directory for files to include when using require, which is
the behavior the book was relying on. Unfortunately, that had some
problems; when ran from a different directory, scripts would crash or
perform differently because the file to require was not
found/different. In 1.9.x require was changed to no longer search the
current directory, and require_relative (which is what you want to use
here) was introduced to require a file relative the the current one.
With Why gone, I don’t think anyone is maintaining the Guide, which is
too bad. It’s one of my favorite programming books.