Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How do I unsplat something?

Reply
Thread Tools

How do I unsplat something?

 
 
Patrick Li
Guest
Posts: n/a
 
      08-12-2008
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-12-2008
On 12.08.2008 18:21, Patrick Li wrote:
> 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
 
Reply With Quote
 
 
 
 
Shadowfirebird
Guest
Posts: n/a
 
      08-12-2008
# this do?

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




On Tue, Aug 12, 2008 at 5:21 PM, Patrick Li <> wrote:
> 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
> --
> Posted via http://www.ruby-forum.com/.
>
>




--
All you can do is try to know who your friends are as you head off to
the war / Pick a star on the dark horizon and follow the light

 
Reply With Quote
 
Patrick Li
Guest
Posts: n/a
 
      08-12-2008
Yup that does nicely. Thanks very much.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
John Barnette
Guest
Posts: n/a
 
      08-12-2008
Hi,

On Tue, Aug 12, 2008 at 9:49 AM, Patrick Li <> wrote:
> 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.

 
Reply With Quote
 
Avdi Grimm
Guest
Posts: n/a
 
      08-12-2008
On Tue, Aug 12, 2008 at 12:39 PM, Shadowfirebird
<> wrote:
> # this do?
>
> def unsplat(x)
> return [x].flatten
> end


Just a warning, #flatten is not the inverse of splat, because it
recurses arbitrary levels of nested array.

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

 
Reply With Quote
 
Patrick Li
Guest
Posts: n/a
 
      08-12-2008
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.

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

 
Reply With Quote
 
Chris Shea
Guest
Posts: n/a
 
      08-12-2008
On Aug 12, 1:14*pm, Patrick Li <patrickli_2...@hotmail.com> wrote:
> 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.
>
> --
> Posted viahttp://www.ruby-forum.com/.


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...by-talk/238693
 
Reply With Quote
 
Shadowfirebird
Guest
Posts: n/a
 
      08-12-2008
# How about:

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

On Tue, Aug 12, 2008 at 8:47 PM, Chris Shea <> wrote:
> On Aug 12, 1:14 pm, Patrick Li <patrickli_2...@hotmail.com> wrote:
>> 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.
>>
>> --
>> Posted viahttp://www.ruby-forum.com/.

>
> 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...by-talk/238693
>
>




--
All you can do is try to know who your friends are as you head off to
the war / Pick a star on the dark horizon and follow the light

 
Reply With Quote
 
Patrick Li
Guest
Posts: n/a
 
      08-12-2008
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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57