LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2010, 03:45 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Question 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?
 
Old 08-31-2010, 04:07 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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 .
 
Old 09-01-2010, 02:38 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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)

Last edited by graemef; 09-01-2010 at 02:41 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Any issues installing Ruby Gems and Ruby on Rails in Slackware? Lufbery Slackware 8 02-09-2011 07:22 PM
Ruby Code Blocks CoderMan Programming 1 05-28-2010 10:39 PM
Ruby SDL/ Ruby OpenGL tutorials/reference? LinuxNoob75 Programming 0 12-30-2007 12:47 PM
[Ruby] Tar'ing or Zip'ing with ruby script lmcilwain Programming 2 01-19-2007 06:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration