Please explain this "Why's" example please

I’m reading Why’s Poignant Guide to Ruby.

I don’t understand this example.

require ‘wordlist’

Get 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

I’m assuming that, if I run the program, there would be a message on the
screen that says “Enter your new idea:” and a text box for me to type
something.

After I type a word (and press an ‘OK’ button?), then what? Can anyone
explain to me the role of Gets here? And also the Each block please. You
may also give me another example with Gets and Each.

Explain it to me as if I’m a moron please. No big words, no complicated
programming terms, none of that. I’m a beginner.

Thanks so much for your time. =)

First things first there wouldn’t be an entry box and an ok button, it
would be on a command line (just text).
gets is a method which when run will wait until the user enters
something and then presses enter and then it returns what the user
enters (so in the example what you enter gets stored in idea (which is a
variable).
each is a very useful method. In this example I assume code_words is a
hash (you put lots of info in the hash, and give each piece of data a
key so you can access it which is more data). For each piece of data the
code between each and end is run (in this case idea.gsub!(real,code))
with real being the key and code being the data.

Example:
If code_words was {“world”=>“planet”, “hello”=>“hi”} and you input hello
world then idea would be made to equal hi planet.
Weirdly this code doesn’t do anything with the idea once it has changed
it but I assume that this is not the whole code or something.

On Sun, Jun 6, 2010 at 12:50 PM, Kaye Ng [email protected] wrote:

After I type a word (and press an ‘OK’ button?), then what? Can anyone
explain to me the role of Gets here? And also the Each block please. You
may also give me another example with Gets and Each.

First of all, it’s “gets” and “each” - ruby is case sensitive :slight_smile:

Okay, so “gets” waits for the user to enter a line of text (that is,
to type in a bunch of characters and then hit enter). It then returns
that text as a string. Here’s an example:

while true
print "say something: "
a = gets
puts “you entered #{a}”
end

To understand “each” you must first understand blocks. Every method in
ruby has an implicit optional argument which is a block of code. The
block is an anonymous function that is called by the method via the
“yield” statement. An example will make it clearer:

def run_a_block(arg1, arg2)
puts “Argument 1 was #{arg1}”
puts “Argument 2 was #{arg2}”
puts “Now going to run the block with arguments foo and 42”
yield [“foo”, 42]
puts “Okay, the block has run, now we are back in the run_a_block
method”
end

run_a_block(“hello”, “world”) do |x, y|
puts “Now we are inside the block. run_a_block passed us arguments
#{x} and #{y}”
end

The “do |x,y| … end” bit is the block. The |x, y| is the argument
list, and means that the calling method is expected to yield a list of
two values. When ‘yield’ is called, control passes from the calling
method to the block, and when it is done it returns to the line after
yield.

Okay, now for “each”. “each” is a method of a collection. It expects a
block, and yields each element of the collection in turn to the block.

list = [1, 2, 4, 8, 16, 31]
list.each do |number|
puts “Got element #{number} from the list”
end

There is one more subtlety in _why’s code example - when a block is
passed a list of several elements, it can either capture them as a
list or as individual elements (this is called “destructuring”). So if
we call each on a hash table, which yields [key, value] pairs, we can
say either

h = {“hello” => “world”, “foo” => “bar”, “baz” => “quux”}
h.each do |pair|
puts “key is #{pair[0]}”
puts “value is #{pair[1]}”
end

or this way

h. each do |key, value|
puts “key is #{key}”
puts “value is #{value}”
end

martin

From Why’s example:


require ‘wordlist’

Get 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


Where is the beginning of ‘end’? Is it ‘require’?
(Like ‘If’ would be the beginning of an If statement (or is it block))

Thanks guys!

On Jun 6, 2010, at 12:20 AM, Kaye Ng wrote:

idea = gets
explain to me the role of Gets here? And also the Each block please. You
may also give me another example with Gets and Each.

Not everything in this world are text boxes and buttons :wink: – there are
good ol’ input and output streams (character based) that are dealt with
by means of print, puts, gets and other methods of Ruby class IO. On
Windows, which is most likely what you use, it is something you would
see in and enter from a cmd.exe window (sometimes called Console).

Gennady.

Difference between puts and print, please? Thanks much!

Also, in Why’s example, the words ‘real’ and ‘code’ are NOT variables,
correct?
Are they just random words that don’t need to be defined? Like can I use
‘monkey’ and ‘ape’ instead?

On 2010-06-07 23:30:40 -0700, Kaye Ng said:

Difference between puts and print, please? Thanks much!

All of these questions (and more) can be answered by reading this
excellent (and free) guide: Programming Ruby: The Pragmatic Programmer's Guide

On Jun 7, 2010, at 11:30 PM, Kaye Ng [email protected] wrote:

Difference between puts and print, please? Thanks much!

Briefly, puts adds a newline at the end but print does not. Try them
both, the difference should be clear.

Ben

On Tue, Jun 8, 2010 at 1:07 AM, Kaye Ng [email protected] wrote:

code_words.each do |real, code|

The beginning is do. do … end. This is called a code block. It is
basically a method that you create in the middle of your code, as you
need
it. The method you are calling can in turn call your block to see how
you
want to handle some particular piece of code that should be custom to
that
one calling of the method. In your case, the method knows how to access
each
of the code words. But it doesn’t know what you want to do with them. So
you
give it a block telling it what you want to do, and as it accesses the
elements, it calls your block for each of them, passing them them into
the
block (your block says it is willing to receive them by placing “real”
and
“code” inside the pipes). In your case, the block looks through your
idea
and when it finds one of the real words, it replaces them with one of
the
code words. This brings up one of the important differences between
blocks
and methods, blocks can interact with the environment they are defined
in.
Methods cannot.

On Tue, Jun 8, 2010 at 1:11 AM, Kaye Ng [email protected] wrote:

Also, in Why’s example, the words ‘real’ and ‘code’ are NOT variables,
correct?
Are they just random words that don’t need to be defined? Like can I use
‘monkey’ and ‘ape’ instead?

Can you think of an easy way to find out? :slight_smile: