Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Writing Good #each Methods?

Reply
Thread Tools

Writing Good #each Methods?

 
 
James Herdman
Guest
Posts: n/a
 
      03-14-2006
I'm working my way through The Ruby Course slides at the moment and am
on the section about dynamicity. Specifically I'm on slide 59 right
now, the slide about a ListItem -- essentially a link list.

So, this got me thinking: what makes a good #each method? I'd love to
hear any pearls of wisdom if any of you have the time.

Thank you,

James H.

 
Reply With Quote
 
 
 
 
Farrel Lifson
Guest
Posts: n/a
 
      03-14-2006
> So, this got me thinking: what makes a good #each method? I'd love to
> hear any pearls of wisdom if any of you have the time.


I think an each method should have the following properties
* All elements must be yielded
* Each element must be yielded exactly once
* If the container has some order associated with it (ie Array or
SortedSet) the elements should be yielded in order
* If there is more than ordering (such as a post, pre or in order
walk round a binary tree) then the elements should be yielded
according to the ordering that is as closest to 'natural' ordering
(inorder in the case of a binary tree). each methods for the other
orderings (each_preorder, each_postorder) must also be provided.

That's my opinion, I'm sure other people might disagree with me here.

Farrel


 
Reply With Quote
 
 
 
 
Paul Battley
Guest
Posts: n/a
 
      03-14-2006
I'd also add
* If appropriate, inspect the arity of the block and provide the
requested number of parameters.

Hash#each's behaviour is a good example:
hash =3D {:a=3D>1, :b=3D>2}
hash.each { |x| p x }
# [:b, 2]
# [:a, 1]
hash.each { |k,v| puts "#{k.inspect} =3D> #{v.inspect}" }
# :b =3D> 2
# :a =3D> 1

Paul.


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-14-2006
Paul Battley wrote:
> I'd also add
> * If appropriate, inspect the arity of the block and provide the
> requested number of parameters.


And another one

* return self

robert

 
Reply With Quote
 
George Ogata
Guest
Posts: n/a
 
      03-14-2006
"Paul Battley" <(E-Mail Removed)> writes:

> I'd also add
> * If appropriate, inspect the arity of the block and provide the
> requested number of parameters.
>
> Hash#each's behaviour is a good example:
> hash = {:a=>1, :b=>2}
> hash.each { |x| p x }
> # [:b, 2]
> # [:a, 1]
> hash.each { |k,v| puts "#{k.inspect} => #{v.inspect}" }
> # :b => 2
> # :a => 1


You don't need to -- just yield the array.

irb(main):001:0> class C
irb(main):002:1> def each
irb(main):003:2> yield [1,2]
irb(main):004:2> yield [3,4]
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> C.new.each{|a| p a}
[1, 2]
[3, 4]
=> nil
irb(main):008:0> C.new.each{|a,b| p [a,b]}
[1, 2]
[3, 4]
=> nil
 
Reply With Quote
 
Paul Battley
Guest
Posts: n/a
 
      03-14-2006
> You don't need to -- just yield the array.

Oops. You are, of course, correct. If it is just an array, that
behaviour comes automatically.

Wouldn't it be nice if inject did the same, though? E.g.
hash.inject([]){ |acc, key, value| ... }

Paul.


 
Reply With Quote
 
George Ogata
Guest
Posts: n/a
 
      03-14-2006
"Paul Battley" <(E-Mail Removed)> writes:

> Wouldn't it be nice if inject did the same, though? E.g.
> hash.inject([]){ |acc, key, value| ... }


You can come close:

hash.inject([]){ |acc, (key, value)| ... }
 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      03-14-2006
Paul Battley schrieb:
> Wouldn't it be nice if inject did the same, though? E.g.
> hash.inject([]){ |acc, key, value| ... }


Paul, you have to use parentheses:

hash.inject([]){ |acc, (key, value)| ... }

Regards,
Pit


 
Reply With Quote
 
Paul Battley
Guest
Posts: n/a
 
      03-14-2006
> hash.inject([]){ |acc, (key, value)| ... }

Thanks! I hadn't thought to try that before. It's definitely going to
make my future use of inject more concise and readable.

Paul.


 
Reply With Quote
 
James H.
Guest
Posts: n/a
 
      03-14-2006
Concerning ordered elements, what is the best way to ascertain the
order wherein the structure is not an array? For instance, suppose you
had a link list. You *could* have a class variable that kept track of
the beginning, but this would mean difficulties when you had more than
one link list in play.

In trying to solve the order issue (naively), I wrote a method like:

def each(&block)
current = self.previous
until current.previous.nil?
current = current.previous
end

# ...
end

This obviously doesn't work because on each call of #each, the focus is
rewound to the intial element in the list.

Any advice on the matter?

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how good is voice writing for computers? LMC Society Computer Information 16 03-16-2007 10:18 PM
Is writing to the event log in Application_BeginRequest a good idea? Nemisis ASP .Net 2 02-28-2007 03:17 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Is Java good for writing simple, yet sleek GUI apps? Love Rhino Java 28 09-25-2004 05:24 PM
Questionare: Which IDE you think is good one for writing Java Applet? M Java 3 06-02-2004 07:39 PM



Advertisments