Prev: threads & mutex question (Rubyist book example)
Next: [ANN] clogger 0.5.0 - body.to_path forwarding
From: Martin DeMello on 6 Jun 2010 04:03 On Sun, Jun 6, 2010 at 12:50 PM, Kaye Ng <sbstn26(a)yahoo.com> 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 :) 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: Gennady Bystritsky on 7 Jun 2010 18:27 On Jun 6, 2010, at 12:20 AM, Kaye Ng wrote: > 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. Not everything in this world are text boxes and buttons ;-) -- 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.
From: Kaye Ng on 8 Jun 2010 02:07 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! -- Posted via http://www.ruby-forum.com/.
From: Kaye Ng on 8 Jun 2010 02:11 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? -- Posted via http://www.ruby-forum.com/.
From: Josh Cheek on 8 Jun 2010 05:13 [Note: parts of this message were removed to make it a legal post.] On Tue, Jun 8, 2010 at 1:07 AM, Kaye Ng <sbstn26(a)yahoo.com> wrote: > 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! > -- > Posted via http://www.ruby-forum.com/. > > 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 <sbstn26(a)yahoo.com> 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? ^_^
|
Pages: 1 Prev: threads & mutex question (Rubyist book example) Next: [ANN] clogger 0.5.0 - body.to_path forwarding |