Forum: Ruby on Rails easy question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
632e61820df714a278b1b08828ab420b?d=identicon&s=25 jack (Guest)
on 2006-06-12 13:39
easy question, but can anyone give me a referance to what 'yield' is?
how to implement, where to use? ..feeling kinda stupid, but thanks in
advance,

j
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-06-12 14:15
jack wrote:
> easy question, but can anyone give me a referance to what 'yield' is?
> how to implement, where to use? ..feeling kinda stupid, but thanks in
> advance,
>
> j

yield inside a method passes control to a supplied block.

so for example, the implementation of Array#each might look something
like:

def each(&block)
  for i in 0...size
    yield self[i]
  end
end

so you can do a.each {|thing| puts thing.to_s}

yield is 'calling' the block ( {|thing| puts thing.to_s} ) with each
item in turn.

Does this make sense ?

Alan
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-06-12 14:16
Alan Francis wrote:
> jack wrote:
>> easy question, but can anyone give me a referance to what 'yield' is?
>> how to implement, where to use? ..feeling kinda stupid, but thanks in
>> advance,
>>
>> j
>

Some useful stuff here:
http://www.rubycentral.com/book/tut_containers.html
4adcca49b3b1e5a08ac202f5d5a9e688?d=identicon&s=25 Guest (Guest)
on 2006-06-12 14:25
Alan Francis wrote:
> jack wrote:
>> easy question, but can anyone give me a referance to what 'yield' is?
>> how to implement, where to use? ..feeling kinda stupid, but thanks in
>> advance,
>>
>> j
>
> yield inside a method passes control to a supplied block.
>
>> Alan
sorry
This topic is locked and can not be replied to.