As botp pointed out, there really isn't much of a difference between the
two. The major difference is how they are used. Essentially, a block
is a method that has no name, is not specifically associated with any
particular object (as far as I'm aware), and can be passed around by
itself as an object (such as "stored in" a variable).
standard method:
Class Foo
def method
# do stuff
end
end
foo = Foo.new
foo.method
block, used with an iterator:
bar.each {|baz| # do stuff }
block, "stored in" a variable:
var = lambda { # do stuff }
var.call
Of course, you could just as easily use the "do . . . end" syntax for
any of these blocks as the brace-delimited syntax. Here's the iterator
example again, with the "do . . . end" syntax:
bar.each do |baz|
# do stuff
end
If that doesn't help clear things up (along with the reading recommended
by botp), you might try being more precise in your point of confusion.
I think that should cover your question, though, if it was as general in
scope as it seemed.
···
On Mon, Apr 09, 2007 at 02:05:06AM +0900, andy wrote:
Hi,
I am trying to learn ruby from the following site.
http://www.rubycentral.com/book/index.html
I am familiar with c# and java, one thing I don't understand is the
difference between code blocks and methods, can anyone offer a better
explanation than the one on the site above ?
any help appreciated.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Ben Franklin: "As we enjoy great Advantages from the Inventions of
others we should be glad of an Opportunity to serve others by any
Invention of ours, and this we should do freely and generously."