Ruby quickies and useful idioms

P

Paul Brannan

irb(main):001:0* foo = %w[ a b c ]
=> ["a", "b", "c"]
irb(main):002:0> bar = %w[ d e f ]
=> ["d", "e", "f"]
irb(main):003:0> baz = Hash[ *( foo.zip( bar ).flatten ) ]
=> {"a"=>"d", "b"=>"e", "c"=>"f"}

Trying to be too succinct can get you into trouble:

irb(main):001:0> a = [1, 2, 3]
=> [1, 2, 3]
irb(main):002:0> b = [[1], [2, 3], [4, 5]]
=> [[1], [2, 3], 3]
irb(main):003:0> Hash[ *( a.zip( b ).flatten ) ]
=> {1=>1, 2=>2, 3=>3, 4=>5}

Paul
 
G

Gavin Kistner

I've found this useful on several occasions

class Array
include EnumerableOperator
def all_pairs
a = size-1
for i,j in product(0..a, 0..a, proc {|i,j| i < j})
yield at(i), at(j)
end
end
end

It's too early in the morning for me to decode this. Is this the same
as my Array#each_unique_pair method documented here:

http://phrogz.net/RubyLibs/rdoc/classes/Array.html#M000061

If so, the implementation which doesn't require anything else is simply:

class Array
def each_unique_pair
self.each_with_index{ |a,i|
self[(i+1)..-1].each{ |b| yield a,b }
}
end
end
 
M

Martin DeMello

Paul Brannan said:
irb(main):001:0* foo = %w[ a b c ]
=> ["a", "b", "c"]
irb(main):002:0> bar = %w[ d e f ]
=> ["d", "e", "f"]
irb(main):003:0> baz = Hash[ *( foo.zip( bar ).flatten ) ]
=> {"a"=>"d", "b"=>"e", "c"=>"f"}

Trying to be too succinct can get you into trouble:

irb(main):001:0> a = [1, 2, 3]
=> [1, 2, 3]
irb(main):002:0> b = [[1], [2, 3], [4, 5]]
=> [[1], [2, 3], 3]
irb(main):003:0> Hash[ *( a.zip( b ).flatten ) ]
=> {1=>1, 2=>2, 3=>3, 4=>5}

A flatten_once would really be a useful method to have around.

martin
 
M

Martin DeMello

Gavin Kistner said:
I've found this useful on several occasions

class Array
include EnumerableOperator
def all_pairs
a = size-1
for i,j in product(0..a, 0..a, proc {|i,j| i < j})
yield at(i), at(j)
end
end
end

It's too early in the morning for me to decode this. Is this the same
as my Array#each_unique_pair method documented here:

http://phrogz.net/RubyLibs/rdoc/classes/Array.html#M000061

If so, the implementation which doesn't require anything else is simply:

class Array
def each_unique_pair
self.each_with_index{ |a,i|
self[(i+1)..-1].each{ |b| yield a,b }
}
end
end

Heh - so it is :) Mine was basically the translation from the list
comprehension.

martin
 
B

Brian Schroeder

Please share your quickies!

A complement to Array each_with_index that I find myself using quite often
when juggling with Arrays. Makes the code more legible.

class Array
def collect_with_index
r = Array.new(length)
each_with_index do | o, i | r = yield(o,i) end r
end
end

greetings,

Brian
 
D

David A. Black

Hi --

Please share your quickies!

A complement to Array each_with_index that I find myself using quite often
when juggling with Arrays. Makes the code more legible.

class Array
def collect_with_index
r = Array.new(length)
each_with_index do | o, i | r = yield(o,i) end r
end
end


This is a perennial favorite :) I still pull in my home-made
version fairly often.


David
 
B

Brian Schroeder

This is a perennial favorite :) I still pull in my home-made
version fairly often.

wow, I learned a new word. perennial. Had to look it up in the dictionary,
but it sounds good ;)

From Merriam-Webster:
Etymology: Latin perennis, from per- throughout + annus year
 
N

Nicholas Van Weerdenburg

Common in gardening- annuals (you have to plant them each year) and
perrenials (they come back- good for lazier gardeners). Unfortunately my
wife didn't realize that perrenials still needed water, or they turn
into sub-annuals :).

Also, "The Perennial Philosophy" by Aldous Huxley (of "Brave New World"
novel fame), which was very influential on the hippie and new age
movement, and the general trend for finding common religous grounds. A
reference to this book was what sent me to the dictionary a couple of
years back :).

Not so influential maybe as his "The Doors of Perception", which was
about his drug taking experience in the 50s and a huge influence on the
drug culture of the 60s. That title was also the source of the name of
the band "The Doors".

Maybe in a few years Ruby will be know as the "perennial scripting
language" :)

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top