Computer Science Canada Block arguments =S |
Author: | MrHippo [ Mon Dec 10, 2007 5:52 pm ] |
Post subject: | Block arguments =S |
Are they defined and used only within a block, or do they have to be pre-determined =S And how do they work in general.. I've been following Why's Poignant Guide, and it's pretty interesting but I can't get comfortable with where he gets the arguments from, though I do understand what's done with them (I think hehe...) Any help please? Thanks in advance! -MrHippo (I also heard that blocks are a major part of understanding Ruby, which is another reason I ask ) |
Author: | MrHippo [ Mon Dec 10, 2007 6:10 pm ] |
Post subject: | Re: Block arguments =S |
To further elaborate my understanding of blocks and the arguments, I'll try to interpret this excerpt from the guide in the way I understand it (not sure how to do the colour stuff for the code :S): require 'wordlist' # Print each idea out with the words fixed Dir['idea-*.txt'].each do |file_name| idea = File.read( file_name ) code_words.each do |real, code| idea.gsub!( code, real ) end puts idea end So the way I see it this is what happens: 1) The Dir class method searches a directory (not sure which =S) for any text file with "idea-" in it. The "wordlist" document is required for the entire program to work. 2) The "each" method goes through the elements within the document(s) and hands them down into the block argument "file_name" within the block. 3) Then, the contents of the "file_name" argument are named with the variable "idea" 4) Next, the program operates with the "code_words" variable from the "wordlist" document. The "each" method goes through each pair in "code_words" (code_words is a hash) and hands them into the "real" and "code" block arguments 5) The "real" elements are substituted (permanently) into the idea. i.e. every word that matches a "code" string is replaced with a "real" string across the idea. 6) The idea (with substituted strings) is printed. Something like that =S Another problem, I just realized, is I'm not sure what to refer to as what. Just not entirely comfortable with definitions and vocabulary I would guess hehe... -MrHippo |
Author: | Tony [ Mon Dec 10, 2007 6:11 pm ] | ||||
Post subject: | RE:Block arguments =S | ||||
blocks are definitely one of the pilars of Ruby. I'm not really sure what your question here is. Blocks could be predifined, or dynamically passed as an argument
In the above example, the block of code
is passed to the foo method, which in turn executes it with yield. |
Author: | MrHippo [ Mon Dec 10, 2007 6:22 pm ] |
Post subject: | Re: Block arguments =S |
My question is something along the lines of "What is a block argument (e.g foo {|x , y| print x + "is greater than" + y} ) I think I might have a flawed understanding of the |x , y| part and where these variables come from. Also, can a block be recorded as a variable and then used later on in variable-form? And, a bit besides the point, what's the difference between "puts" and "print"? |
Author: | Tony [ Mon Dec 10, 2007 6:48 pm ] | ||||
Post subject: | RE:Block arguments =S | ||||
when you call a block (yield) you can pass it some arguments. those arguments get applied to |argument, another| part. Those live within the scope of the block supplied.
This would be functionally equivelent of having
except that now it's all inline and dynamic |
Author: | MrHippo [ Tue Dec 11, 2007 10:45 pm ] |
Post subject: | RE:Block arguments =S |
Thanks =D I read into it a little more as well and I think I got it. Well.. on a basic level at least |