Please explain this "Why's" example please

K

Kaye Ng

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. =)
 
A

Angus Hammond

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

Martin DeMello

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

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
 
G

Gennady Bystritsky

I'm reading Why's Poignant Guide to Ruby.
=20
I don't understand this example.
--------------------------------------------------------
require 'wordlist'
=20
# Get evil idea and swap in code words
=20
print "Enter your new idea: "
=20
idea =3D gets
=20
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.
=20
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.

Not everything in this world are text boxes and buttons ;-) -- there are go=
od ol' input and output streams (character based) that are dealt with by me=
ans of print, puts, gets and other methods of Ruby class IO. On Windows, wh=
ich is most likely what you use, it is something you would see in and enter=
from a cmd.exe window (sometimes called Console).

Gennady.
 
K

Kaye Ng

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!
 
K

Kaye Ng

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?
 
B

Ben Bleything

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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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

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? ^_^
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top