Running wordlist.rb code from "why's (poignant) Guide to Ruby"

Hi,

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

···

--------------------------

Running code.rb gives errors:

./code.rb:13: syntax error, unexpected ',', expecting ')'
  File::open ( "idea-" + idea_name + ".txt", "w" ) do |f|
                                            ^
./code.rb:13: syntax error, unexpected ')', expecting $end
  File::open ( "idea-" + idea_name + ".txt", "w" ) do |f|
                                                  ^

Any help would be great.

--
Posted via http://www.ruby-forum.com/.

Okay I got it working now:

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?'
}

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 (
2: Full file path is needed in the "require" line
3: code_words needs to be changed to Code_words
(http://www.ruby-forum.com/topic/1873204)

--
Posted via http://www.ruby-forum.com/.

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:

  Ruby Gotchas - Google Slides

for a simpler example. Check out the rest of the slides while you're
there; they're from my Ruby Gotchas presentation. :slight_smile:

-Dave

···

On Mon, Jun 24, 2013 at 2:02 AM, Arslan Farooq <lists@ruby-forum.com> wrote:

1: There should be no space between File::open and (

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.

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.

Walton

···

On 6/24/2013 12:02 AM, Arslan Farooq wrote:

2: Full file path is needed in the "require" line

Dave Aronson wrote in post #1113422:
[...]

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. :slight_smile:

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 Hoops 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.

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 Pine also looks good.
Although I'm not sure which one should I start with.

···

--
Posted via http://www.ruby-forum.com/\.

"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.

Regards,
Marcus

···

Am 25.06.2013 07:52, schrieb Arslan Farooq:

Dave Aronson wrote in post #1113422:
[...]

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. :slight_smile:

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 Hoops 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.

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 Pine also looks good.
Although I'm not sure which one should I start with.

--
<https://github.com/stomar/&gt;

Thanks Marcus!

Well, I have started reading "The Well-Grounded Rubyist" :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.