[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Hash#map_pairs

jeff_alexander_44

3/24/2007 3:24:00 PM

class Hash
def map_pairs
result = Array.new
self.each_pair { |k, v|
result << yield(k, v)
}
result
end
end

h = {
"a" => "x",
"b" => "y",
}
puts h.map_pairs { |k, v| "#{k} => #{v}" }.join(", ")

I was surprised to discover there was no Hash#map_pairs. I have found
that chaining transformations is usually better than the
iterative/imperative style implied by Hash#each_pair.

On second thought, it looks like this does the same thing:

puts h.to_a.map { |k, v| "#{k} => #{v}" }.join(", ")

But this fails to convey the programmer's idea in the same way that
map_pairs does. It is also needlessly inefficient, using a whole
extra temp array.

9 Answers

Ara.T.Howard

3/24/2007 3:44:00 PM

0

jeff_alexander_44

3/24/2007 3:52:00 PM

0

Aha, thanks.

I was fooled by ri's output. My eyes went straight to the "Instance
methods" section. Since "map" is an instance method, shouldn't it be
listed under "Instance methods"? I now see that it's listed under
"Includes" --- which is fine, in addition to "Instance methods".

Chris Carter

3/24/2007 4:01:00 PM

0

On 3/24/07, jeff_alexander_44@yahoo.com <jeff_alexander_44@yahoo.com> wrote:
> Aha, thanks.
>
> I was fooled by ri's output. My eyes went straight to the "Instance
> methods" section. Since "map" is an instance method, shouldn't it be
> listed under "Instance methods"? I now see that it's listed under
> "Includes" --- which is fine, in addition to "Instance methods".
>
>
>
It is mixed in from the Enumerable module. It has lots of cool
functions. You should check it out, since Hash has them all as well
as Array.

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

Ara.T.Howard

3/24/2007 4:03:00 PM

0

jeff_alexander_44

3/24/2007 4:23:00 PM

0


$ irb
irb(main):001:0> Hash.instance_methods.include? "map"
=> true

So one would expect "map" to appear under "Instance methods" in ri.
Of course it comes from a mixin. That's not the issue here. The
existence of this thread is a data point.

Thus ensues the eternal struggle between those who are accustomed to
the idiosyncrasies and those who are driven insane by them.

Ara.T.Howard

3/24/2007 4:50:00 PM

0

jeff_alexander_44

3/24/2007 5:20:00 PM

0

Person A: "Here is a needless idiosyncrasy: ..."
Person B: "I can produce apparent idiosyncratic behavior with
method_missing."
Person A: "Well, of course. But what about the idiosyncrasy I
mentioned?"

The struggle continues...

Rick DeNatale

3/26/2007 10:18:00 PM

0

On 3/24/07, jeff_alexander_44@yahoo.com <jeff_alexander_44@yahoo.com> wrote:
> class Hash
> def map_pairs
> result = Array.new
> self.each_pair { |k, v|
> result << yield(k, v)
> }
> result
> end
> end
>
> h = {
> "a" => "x",
> "b" => "y",
> }
> puts h.map_pairs { |k, v| "#{k} => #{v}" }.join(", ")
>
> I was surprised to discover there was no Hash#map_pairs. I have found
> that chaining transformations is usually better than the
> iterative/imperative style implied by Hash#each_pair.
>

In Ruby 1.9 , at least as it stands today, 'each-like' methods like
each_pair will return an Enumerator if you don't give them a block,
this allows a nice ability to chain transformations.

rick@frodo:/public/rubysource/ruby1.8.5$ irb1.9
irb(main):001:0> {:a => 1, :b => 2}.each_pair
=> #<Enumerable::Enumerator:0xb7bd2650>
irb(main):002:0> {:a => 1, :b => 2}.each_pair.map {|k,v| [v,k]}
=> [[2, :b], [1, :a]]
irb(main):003:0>


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Matthew Rudy Jacobs

7/18/2007 9:18:00 AM

0

I've been looking at this of late,

what I want is ;

class Hash
def map_pairs
new_hash = self.class.new # works in HashWithIndifferentAccess
self.each do |key, value|
new_key, new_value = yield(key, value)
new_hash[new_key] = new_value
end
return new_hash
end

def map_values
new_hash = self.class.new
self.each do |key, value|
new_hash[key] = yield(key, value)
end
return new_hash
end
end

is that a bad idea?
does "map" have to return an array?

--
Posted via http://www.ruby-....