Hi. all
there are two array.
a = [1, 2]
b = [3, 4]
When I execute the following statement, what can you guess as a result?
irb:$> a.insert(a.length, *b)
very funny
Hi. all
there are two array.
a = [1, 2]
b = [3, 4]
When I execute the following statement, what can you guess as a result?
irb:$> a.insert(a.length, *b)
very funny
It does exactly what Iâd expect. The insert line expands to:
a.insert(2, 3, 4) # a.length, b[0], b[1]
and so it inserts 3 and 4 at the end of the array. (It inserts before
element #n, but since a has only elements #0 and #1, asking to insert
before element #2 is the same as asking to insert at the end of the
list)
irb(main):001:0> a = [1,2]
=> [1, 2]
irb(main):002:0> b = [3,4]
=> [3, 4]
irb(main):003:0> a.insert(a.length, *b)
=> [1, 2, 3, 4]
irb(main):004:0> a
=> [1, 2, 3, 4]
Of course, a += b is a simpler way to write this.
Do you get something different? I am using ruby-1.8.6p114 under Ubuntu
Hardy.
On Thu, Jan 8, 2009 at 8:51 AM, Jun Y. Kim [email protected]
wrote:
Hi. all
there are two array.
a = [1, 2]
b = [3, 4]
When I execute the following statement, what can you guess as a result?
irb:$> a.insert(a.length, *b)
very funny
I didnât get the joke.
Brian C. wrote:
irb(main):003:0> a.insert(a.length, *b)
=> [1, 2, 3, 4]
irb(main):004:0> a
=> [1, 2, 3, 4]Of course, a += b is a simpler way to write this.
Actually a.concat(b) is the simpler way to write this. a += b does
something
slightly different (create a third array [1,2,3,4] and then assign that
to a)
Sebastian H. wrote:
Actually a.concat(b) is the simpler way to write this. a += b does
something
slightly different (create a third array [1,2,3,4] and then assign that
to a)
Yes of course, sorry about that.
However I still donât see what the OP found funnyâŠ
thanks for your reply, brian.
whatâs the definition of â*â?
is this pointer?
Iâve never heard about âpointerâ IN RUBY.
Am I wrong?
Jun Y. Kim wrote:
whatâs the definition of â*â?
is this pointer?
No, itâs the âsplatâ operator. It expands an array into a list of
arguments.
So: foo(*[1,2,3]) == foo(1,2,3) and a.insert(2, *[3,4]) ==
a.insert(2,3,4)
HTH,
Sebastian
Jun Y. Kim wrote:
whatâs the definition of â*â?
That question was answered yesterday. See
http://www.ruby-forum.com/topic/175040
and follow the link to information on the splat operator.
is this pointer?
No. You are probably thinking of C. Ruby is a different programming
language.
Iâve never heard about âpointerâ IN RUBY.
Me neither. But everything in Ruby is a reference, which is sort-of like
a pointer. But you donât do any explicit dereferencing like you would
for a pointer in C.
http://www.ruby-forum.com/topic/174962
Am I wrong?
About what? That a.insert(a.length, *b) is somehow funny?
Hi!
2009/1/8 Jun Y. Kim [email protected]:
whatâs the definition of â*â?
is this pointer?
Itâs called âsplat operatorâ. You can use it to âexplodeâ your array
elements or join some variables on an array. Note the difference:
irb(main):001:0> a = [1,2]; b = [3,4]
=> [3, 4]
irb(main):002:0> a.push(b)
=> [1, 2, [3, 4]]
irb(main):003:0> a.push(*b)
=> [1, 2, [3, 4], 3, 4]
And now:
irb(main):004:0> def test(*args)
irb(main):005:1> p args
irb(main):006:1> end
=> nil
irb(main):007:0> test(1)
[1]
=> nil
irb(main):008:0> test(1,2,3)
[1, 2, 3]
Regards,
thanks for all your replies.
Hi â
On Thu, 8 Jan 2009, EustĂĄquio Rangel wrote:
Hi!
2009/1/8 Jun Y. Kim [email protected]:
whatâs the definition of â*â?
is this pointer?Itâs called âsplat operatorâ. You can use it to âexplodeâ your array
elements or join some variables on an array. Note the difference:
AKA the âunary unarrayâ operator
David
â
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (The Well-Grounded Rubyist)
http://www.wishsight.com => Independent, social wishlist management!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs