Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Block arguments =S
Index -> Programming, Ruby -> Ruby Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MrHippo




PostPosted: 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? Smile

Thanks in advance!

-MrHippo

(I also heard that blocks are a major part of understanding Ruby, which is another reason I ask )
Sponsor
Sponsor
Sponsor
sponsor
MrHippo




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




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

irb(main):001:0> def foo(&block)
irb(main):002:1> yield block
irb(main):003:1> end
=> nil
irb(main):004:0> foo {puts "bar"}
bar
=> nil

In the above example, the block of code
Ruby:

puts "bar"

is passed to the foo method, which in turn executes it with yield.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
MrHippo




PostPosted: 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"?
Tony




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

code:

irb(main):001:0> def foo
irb(main):002:1> yield 42
irb(main):003:1> end
=> nil
irb(main):004:0> foo {|x| puts x}
42
=> nil


This would be functionally equivelent of having
Ruby:

def my_block(x)
   puts x
end

def foo
   my_block(42)
end

except that now it's all inline and dynamic
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
MrHippo




PostPosted: 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 Razz
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: