LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Ruby blocks (https://www.linuxquestions.org/questions/programming-9/ruby-blocks-829605/)

MTK358 08-31-2010 03:45 PM

Ruby blocks
 
I often see these blocks after a method call like this:

Code:

File::open('filename', 'w') do |f|
    f << string
end

What does this actually mean on a low level?

Sergei Steshenko 08-31-2010 04:07 PM

Quote:

Originally Posted by MTK358 (Post 4083981)
I often see these blocks after a method call like this:

Code:

File::open('filename', 'w') do |f|
    f << string
end

What does this actually mean on a low level?

From the very first page of Yahoo matches in response to "Ruby block": http://c2.com/cgi/wiki?BlocksInRuby .

graemef 09-01-2010 02:38 AM

How "low level" do you want to go?

The File::open method will yield the File object, this allows the block to use this object, once the block is closed the File::open method will then close the file. Because the File::open method has just a single yield the block will be executed once, but if within the method the yield is wrapped inside a while then you will get an iterator, as is the case with the IO.each (which File will inherit)


All times are GMT -5. The time now is 05:05 PM.