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

**URL:** <https://www.ruby-forum.com/t/running-wordlist-rb-code-from-whys-poignant-guide-to-ruby/235469>\
**Category:** Ruby\
**Created:** [June 24, 2013, 7:28am UTC](https://www.ruby-forum.com/t/running-wordlist-rb-code-from-whys-poignant-guide-to-ruby/235469 "2013-06-24T07:28:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [June 24, 2013, 7:28am UTC](https://www.ruby-forum.com/t/running-wordlist-rb-code-from-whys-poignant-guide-to-ruby/235469/1 "2013-06-24T07:28:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![arslan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@arslan](https://www.ruby-forum.com/u/arslan)\
**Post date:** [June 24, 2013, 8:02am UTC](https://www.ruby-forum.com/t/running-wordlist-rb-code-from-whys-poignant-guide-to-ruby/235469/2 "2013-06-24T08:02:48Z")

</div>

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  
([Why's (poignant) Guide to Ruby - code question - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/1873204))

---

<div class="post-metadata">

**Author:** ![arslan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@arslan](https://www.ruby-forum.com/u/arslan)\
**Post date:** [June 24, 2013, 2:00pm UTC](https://www.ruby-forum.com/t/running-wordlist-rb-code-from-whys-poignant-guide-to-ruby/235469/3 "2013-06-24T14:00:08Z")

</div>

On Mon, Jun 24, 2013 at 2:02 AM, Arslan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

[Ruby Gotchas - Google Slides](https://docs.google.com/presentation/d/1cqdp89_kolr4q1YAQaB-6i5GXip8MHyve8MvQ_1r6_s/#slide=id.gd9ccd329_00)

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

-Dave

---

<div class="post-metadata">

**Author:** ![arslan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@arslan](https://www.ruby-forum.com/u/arslan)\
**Post date:** [June 25, 2013, 7:52am UTC](https://www.ruby-forum.com/t/running-wordlist-rb-code-from-whys-poignant-guide-to-ruby/235469/4 "2013-06-25T07:52:55Z")

</div>

Dave A. 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. 🙂

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.

---

<div class="post-metadata">

**Author:** ![arslan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@arslan](https://www.ruby-forum.com/u/arslan)\
**Post date:** [June 25, 2013, 6:45pm UTC](https://www.ruby-forum.com/t/running-wordlist-rb-code-from-whys-poignant-guide-to-ruby/235469/5 "2013-06-25T18:45:36Z")

</div>

Am 25.06.2013 07:52, schrieb Arslan F.:

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

Regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![arslan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@arslan](https://www.ruby-forum.com/u/arslan)\
**Post date:** [June 24, 2013, 3:51pm UTC](https://www.ruby-forum.com/t/running-wordlist-rb-code-from-whys-poignant-guide-to-ruby/235469/6 "2013-06-24T15:51:43Z")

</div>

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

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

Walton

---

<div class="post-metadata">

**Author:** ![arslan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@arslan](https://www.ruby-forum.com/u/arslan)\
**Post date:** [June 27, 2013, 2:42pm UTC](https://www.ruby-forum.com/t/running-wordlist-rb-code-from-whys-poignant-guide-to-ruby/235469/7 "2013-06-27T14:42:02Z")

</div>

Thanks Marcus!

Well, I have started reading “The Well-Grounded Rubyist” 🙂
