ruby one-liner needed

K

Kev Jackson

Here's one taht I know is possible (I'm sure I've seen it somewhere before)

I have 2 arrays and I'd like to change them into a Hash
stuff = ["1", "2", "3"]
Item.column_names.each { |column_name| new_item[column_name] = stuff[?] }

The problem is how to index within the block to move along the stuff
array, and how to deal with the fact that most of the time the stuff
array is shorter than the column_names array

I'm sure using collect, map of something else ruby-ish that this is more
than possible, but I can't find a solution right now

Kev
 
S

Simon Strandgaard

I have 2 arrays and I'd like to change them into a Hash


pairs=3D%w(a b c d).zip(%w(x y));Hash[*pairs.flatten]
=3D> {"a"=3D>"x", "b"=3D>"y", "c"=3D>nil, "d"=3D>nil}
 
J

Johannes Friestad

I'm sure there are many ways to do it, here's one:

a=3D[3,4,5,6]
b=3D%w{three four five six seven}
res=3D{}
a.length.times {|i| break if i>=3Db.length; res[a]=3Db; }

It handles the situation where either array is shorter than the other.

jf

Here's one taht I know is possible (I'm sure I've seen it somewhere befor= e)

I have 2 arrays and I'd like to change them into a Hash
stuff =3D ["1", "2", "3"]
Item.column_names.each { |column_name| new_item[column_name] =3D stuff[?]= }

The problem is how to index within the block to move along the stuff
array, and how to deal with the fact that most of the time the stuff
array is shorter than the column_names array

I'm sure using collect, map of something else ruby-ish that this is more
than possible, but I can't find a solution right now

Kev
 
K

Kev Jackson

Johannes said:
I'm sure there are many ways to do it, here's one:

a=[3,4,5,6]
b=%w{three four five six seven}
res={}
a.length.times {|i| break if i>=b.length; res[a]=b; }

It handles the situation where either array is shorter than the other.

jf

I'm using...

new_item = Hash.new
Item.column_names.each { |column_name| new_item[column_name] = row.shift }

at the moment and it's working, so I won't replace it with these suggestions

zip was the method I knew about but couldn't remember :)

Kev
 
W

William James

Kev said:
Here's one taht I know is possible (I'm sure I've seen it somewhere before)

I have 2 arrays and I'd like to change them into a Hash
stuff = ["1", "2", "3"]
Item.column_names.each { |column_name| new_item[column_name] = stuff[?] }

The problem is how to index within the block to move along the stuff
array, and how to deal with the fact that most of the time the stuff
array is shorter than the column_names array

stuff = ["1", "2", "3"]
Item.column_names.each_with_index { |column_name, i|
new_item[column_name] = stuff }
 
R

Ross Bamford

Here's one taht I know is possible (I'm sure I've seen it somewhere
before)

I have 2 arrays and I'd like to change them into a Hash
stuff = ["1", "2", "3"]
Item.column_names.each { |column_name| new_item[column_name] = stuff[?] }

The problem is how to index within the block to move along the stuff
array, and how to deal with the fact that most of the time the stuff
array is shorter than the column_names array

I'm sure using collect, map of something else ruby-ish that this is more
than possible, but I can't find a solution right now

Kev

Well, theres lots of ways, but one is to inject the Hash as someone here
showed me yesterday:

items.column_names.inject({}) { |hash, column_name| hash[column_name] =
row.shift ; hash }

(I wanted to eliminate the '; hash' at the end of the closure but couldn't
find a way yet...)

However, I personally prefer Simon's solution, which can be a real
one-liner too :)

Hash[*items.column_names.zip(row).flatten]
 
D

David A. Black

Hi --

Here's one taht I know is possible (I'm sure I've seen it somewhere before)

I have 2 arrays and I'd like to change them into a Hash
stuff = ["1", "2", "3"]
Item.column_names.each { |column_name| new_item[column_name] = stuff[?] }

The problem is how to index within the block to move along the stuff array,
and how to deal with the fact that most of the time the stuff array is
shorter than the column_names array

I'm sure using collect, map of something else ruby-ish that this is more than
possible, but I can't find a solution right now

a = [1,2,3]
b = [4,5,6,7,8]
Hash[*a.zip(b[0,a.size]).flatten
=> {1=>4, 2=>5, 3=>6}

The problem with flatten is that it can be overzealous, but it looks
like you probably don't have nested arrays. If you do, you can get
the flattenx package from RAA and use flatten_once.


David
 
D

David A. Black

Hi --

Hi --

Here's one taht I know is possible (I'm sure I've seen it somewhere before)

I have 2 arrays and I'd like to change them into a Hash
stuff = ["1", "2", "3"]
Item.column_names.each { |column_name| new_item[column_name] = stuff[?] }

The problem is how to index within the block to move along the stuff array,
and how to deal with the fact that most of the time the stuff array is
shorter than the column_names array

I'm sure using collect, map of something else ruby-ish that this is more
than possible, but I can't find a solution right now

a = [1,2,3]
b = [4,5,6,7,8]
Hash[*a.zip(b[0,a.size]).flatten
=> {1=>4, 2=>5, 3=>6}

Actually just use the shorter one first and zip won't fill it out with
nils:

Hash[*a.zip(b).flatten]


David
 
D

David A. Black

Hi --

Hi --

Hi --

Here's one taht I know is possible (I'm sure I've seen it somewhere
before)

I have 2 arrays and I'd like to change them into a Hash
stuff = ["1", "2", "3"]
Item.column_names.each { |column_name| new_item[column_name] = stuff[?] }

The problem is how to index within the block to move along the stuff
array, and how to deal with the fact that most of the time the stuff array
is shorter than the column_names array

I'm sure using collect, map of something else ruby-ish that this is more
than possible, but I can't find a solution right now

a = [1,2,3]
b = [4,5,6,7,8]
Hash[*a.zip(b[0,a.size]).flatten
=> {1=>4, 2=>5, 3=>6}

Actually just use the shorter one first and zip won't fill it out with
nils:

Hash[*a.zip(b).flatten]

I hereby swear I will stop posting until I am being torn in no more
than ten directions at the same time :) In other words: I realize
that you can't just switch them around at will. So you have to decide
whether you're happy to have nils or if you want to truncate the hash
based on available values.

So adapt as needed :)


David

P.S. One of the things I've been grappling with today is (the third
day of) intermittent/non-existent Internet service -- and a mysterious
reboot on my server. So I got and read this thread out of order.
Sorry if I reinvented anyone's wheel :)
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top