How do I unsplat something?

P

Patrick Li

Hi,
Is there any easy to unsplat an item?
ie. 1 becomes [1]
[2,3] remains [2,3] ?

so far i found:
a = *[1]
*b = *a #=> [1]
a = *[1,2]
*b = *a #=> [1,2]

but this doesn't work if a is a hash, because hash overrides the to_a
method
a = *[{"a"=>1}]
*b = *a #=> [["a",1]] when i actually want: [{"a"=>1}]

Thanks for helping
-Patrick
 
R

Robert Klemme

Is there any easy to unsplat an item?
ie. 1 becomes [1]
[2,3] remains [2,3] ?

so far i found:
a = *[1]
*b = *a #=> [1]
a = *[1,2]
*b = *a #=> [1,2]

but this doesn't work if a is a hash, because hash overrides the to_a
method
a = *[{"a"=>1}]
*b = *a #=> [["a",1]] when i actually want: [{"a"=>1}]

What result are you trying to get?

robert
 
S

Shadowfirebird

# this do?

def unsplat(x)
return [x].flatten
end




Hi,
Is there any easy to unsplat an item?
ie. 1 becomes [1]
[2,3] remains [2,3] ?

so far i found:
a = *[1]
*b = *a #=> [1]
a = *[1,2]
*b = *a #=> [1,2]

but this doesn't work if a is a hash, because hash overrides the to_a
method
a = *[{"a"=>1}]
*b = *a #=> [["a",1]] when i actually want: [{"a"=>1}]

Thanks for helping
-Patrick
 
J

John Barnette

Hi,

Yup that does nicely. Thanks very much.

Are you sure you want to flatten that far? Why not just arrayify?

Array(1) # => 1
Array([2,3]) # => [2,3]
Array([4,[5,6]]) # => [4,[5,6]]


~ j.
 
P

Patrick Li

I can't use Array() because I want:
{"a"=>2} to become [{"a"=>2}]

so far: flatten is the best so far... except that it recursively
flattens inner arrays too... which is not ideal but workable for my
current use.
 
C

Chris Shea

I can't use Array() because I want:
{"a"=>2} to become [{"a"=>2}]

so far: flatten is the best so far... except that it recursively
flattens inner arrays too... which is not ideal but workable for my
current use.

There was a ruby quiz that included implementations of a single-level
flatten[1]. Taking from James Gray's solution[2], and minding your
requirements regarding hashes, what about this:

def unsplat(the_splatted)
if the_splatted.kind_of?(Hash)
[the_splatted]
else
[the_splatted].inject(Array.new) { |arr, a| arr.push(*a) }
end
end

unsplat(1) # => [1]
unsplat([2,3]) # => [2, 3]
unsplat([4,[5,6]]) # => [4, [5, 6]]
unsplat({'a'=>2}) # => [{"a"=>2}]

HTH,
Chris

[1] http://www.rubyquiz.com/quiz113.html
[2] http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/238693
 
S

Shadowfirebird

# How about:

def unsplat(thing)
if thing.kind_of?(Array)
thing
else
[thing]
end
end

I can't use Array() because I want:
{"a"=>2} to become [{"a"=>2}]

so far: flatten is the best so far... except that it recursively
flattens inner arrays too... which is not ideal but workable for my
current use.

There was a ruby quiz that included implementations of a single-level
flatten[1]. Taking from James Gray's solution[2], and minding your
requirements regarding hashes, what about this:

def unsplat(the_splatted)
if the_splatted.kind_of?(Hash)
[the_splatted]
else
[the_splatted].inject(Array.new) { |arr, a| arr.push(*a) }
end
end

unsplat(1) # => [1]
unsplat([2,3]) # => [2, 3]
unsplat([4,[5,6]]) # => [4, [5, 6]]
unsplat({'a'=>2}) # => [{"a"=>2}]

HTH,
Chris

[1] http://www.rubyquiz.com/quiz113.html
[2] http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/238693
 
P

Patrick Li

lol yeah. Shadowfirebird's hack is the one i'm currently using. I just
wanted to know if there was a easier one-liner way of doing it. It
seemed plausible for there to be reverse operator given a splat
operator.

Anyway thanks for the help
-Patrick
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,660
Latest member
vidotip479

Latest Threads

Top