Lookup, a.collect{|i|b[i]}

J

John Carter

# Rubyable email..
#I suspect there is a standard trick for this, but can't spot it now...

a = [3,6,3,1,6] # Arbitary Array of stuff
b = [1,2,3,4,5,6,7,8,9,0] # Could be Array or Hash, don't care.
p a.collect{|i|b}
# Outputs...
#[4, 7, 4, 2, 7]

# I vaguely feel that there should be a standard method "lookup" and
# perhaps a standard module Indexable.
# Like module Enumerable assumes there exists an "each" method,
# module Indexable assume it's including class has an [] method.
module Indexable
def lookup(*ary)
ary.collect{|i| self}
end
end

class Array
include Indexable
end

class Hash
include Indexable
end

p b.lookup( 3,6,3,1,6)
# outputs...
#[4, 7, 4, 2, 7]

h = {'tom'=>1, 'dick'=>2, 'harry'=>3}
p h.lookup(*%w{tom tom dick harry dick})
# Outputs...
#[1, 1, 2, 3, 2]

#John Carter Phone : (64)(3) 358 6639
#Tait Electronics Fax : (64)(3) 359 4632
#PO Box 1645 Christchurch Email : (e-mail address removed)
#New Zealand

#Refactorers do it a little better every time.
 
W

WATANABE Hirofumi

Hi,

John Carter said:
p b.lookup( 3,6,3,1,6)
# outputs...
#[4, 7, 4, 2, 7]

h = {'tom'=>1, 'dick'=>2, 'harry'=>3}
p h.lookup(*%w{tom tom dick harry dick})
# Outputs...
#[1, 1, 2, 3, 2]

% irb
a = [3,6,3,1,6] => [3, 6, 3, 1, 6]
b = [1,2,3,4,5,6,7,8,9,0] => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
b.values_at(*a) => [4, 7, 4, 2, 7]
h = {'tom'=>1, 'dick'=>2, 'harry'=>3} => {"harry"=>3, "dick"=>2, "tom"=>1}
h.values_at(*%w{tom tom dick harry dick})
=> [1, 1, 2, 3, 2]
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Lookup, a.collect{|i|b}"

|a = [3,6,3,1,6] # Arbitary Array of stuff
|b = [1,2,3,4,5,6,7,8,9,0] # Could be Array or Hash, don't care.
|p a.collect{|i|b}
|# Outputs...
|#[4, 7, 4, 2, 7]

How about b.values_at(*a) ?

matz.
 
T

Trans

I think #[] would have been better if it worked this way and the
ary[start, length] notation was left to another method (#slice), or
another notation.

T.
 
L

Lankad

Why did you put a *?

b.values_at(*a)


Thanks


WATANABE Hirofumi said:
Hi,

John Carter said:
p b.lookup( 3,6,3,1,6)
# outputs...
#[4, 7, 4, 2, 7]

h = {'tom'=>1, 'dick'=>2, 'harry'=>3}
p h.lookup(*%w{tom tom dick harry dick})
# Outputs...
#[1, 1, 2, 3, 2]

% irb
a = [3,6,3,1,6] => [3, 6, 3, 1, 6]
b = [1,2,3,4,5,6,7,8,9,0] => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
b.values_at(*a) => [4, 7, 4, 2, 7]
h = {'tom'=>1, 'dick'=>2, 'harry'=>3} => {"harry"=>3, "dick"=>2, "tom"=>1}
h.values_at(*%w{tom tom dick harry dick})
=> [1, 1, 2, 3, 2]
 
D

David A. Black

Hi --

Why did you put a *?

b.values_at(*a)

* is the unar[r]ay (unary unarray :) operator. For example:

a = [1,2,3]
some_method(a) => same as some_method([1,2,3])
some_method(*a) => same as some_method(1,2,3)


David
 
J

John Carter

How about b.values_at(*a) ?

Exactly what I want! I knew it was in there somewhere, but somehow I
overlooked it twice. (Twice, once for Hash and once for Array)

Sorry for the dumb question, thanks for the kind answer.

John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand

Refactorers do it a little better every time.
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top