chucker
2006-01-29, 09:33
I've been going through this (http://www.math.umd.edu/~dcarrera/ruby/0.3/) Ruby tutorial (again), which, while it has some flaws and things I would starkly disagree with, is still a reasonably good course.
But there's just one thing I don't get, and that's "yield". It's first used in this (http://www.math.umd.edu/~dcarrera/ruby/0.3/chp_04/iterators.html) section, the first example being:
def twice
yield
yield
end
twice { puts "Hello World" }
As much as I understand what it does, why it does it and how it works, I just can't stop thinking how... absurd this is. I mean, here, you have a line of code (twice {..}) that calls up a method, twice. But the method doesn't contain the actual code to wane through, no, it just "yields" the code. Twice. Huh? Isn't that the very opposite way of how any logical person would do it?
It gets really bad, though, when the author gets to this example:
class AddressBook
def each
@persons.each { |p| yield p }
end
end
:err:
As a programmer friend of mine said, "it's like return... but... only not...".
I'm sorry, but this "yield" thing occurs to me as incredibly queer. Am I missing something?
But there's just one thing I don't get, and that's "yield". It's first used in this (http://www.math.umd.edu/~dcarrera/ruby/0.3/chp_04/iterators.html) section, the first example being:
def twice
yield
yield
end
twice { puts "Hello World" }
As much as I understand what it does, why it does it and how it works, I just can't stop thinking how... absurd this is. I mean, here, you have a line of code (twice {..}) that calls up a method, twice. But the method doesn't contain the actual code to wane through, no, it just "yields" the code. Twice. Huh? Isn't that the very opposite way of how any logical person would do it?
It gets really bad, though, when the author gets to this example:
class AddressBook
def each
@persons.each { |p| yield p }
end
end
:err:
As a programmer friend of mine said, "it's like return... but... only not...".
I'm sorry, but this "yield" thing occurs to me as incredibly queer. Am I missing something?