[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

define an each iterator, etc.

Guillaume Nargeot

2/26/2007 1:42:00 AM

I would like to know if it's possible to define iterators like each for a
class you have written ?
Is it also possible to write methods that can have the following
behavior/usage:

object.method do |var|
# something
end
2 Answers

John Mettraux

2/26/2007 1:50:00 AM

0

2007/2/26, Guillaume Nargeot
<guillaume.nargeotDONOTFUCKINGSPAM@fusionsystems.co.jp>:
> I would like to know if it's possible to define iterators like each for a
> class you have written ?
> Is it also possible to write methods that can have the following
> behavior/usage:
>
> object.method do |var|
> # something
> end

Hi,

see http://www.rubycentral.com/book/tut_conta...

look for "blocks and iterators"


Best regards,

--
John Mettraux -///- http://jmettraux.o...

dblack

2/26/2007 1:52:00 AM

0

Hi --

On 2/25/07, Guillaume Nargeot
<guillaume.nargeotDONOTSPAM@fusionsystems.co.jp> wrote:
> I would like to know if it's possible to define iterators like each for a
> class you have written ?

Sure. And if you define each and include Enumerator, you get all sorts
of methods along with it (find, select, inject, etc.).

> Is it also possible to write methods that can have the following
> behavior/usage:
>
> object.method do |var|
> # something
> end

Yes; such a method is called an iterator :-)

def object.m
yield 10
end

object.m do |var|
# var is equal to 10
end

Have a look in any Ruby tutorial book for more.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning...)
(See what readers are saying! http://www.r.../r...)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.r...)