[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Can you guess what it will be happened?

? ??

1/8/2009 10:52:00 AM

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 :)

9 Answers

Brian Candler

1/8/2009 11:06:00 AM

0

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.
--
Posted via http://www.ruby-....

Eustaquio 'TaQ' Rangel

1/8/2009 11:08:00 AM

0

On Thu, Jan 8, 2009 at 8:51 AM, Jun Young Kim <jykim@altibase.com> 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. :-)

Sebastian Hungerecker

1/8/2009 11:21:00 AM

0

Brian Candler 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)

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

? ??

1/8/2009 11:26:00 AM

0

thanks for your reply, brian.

what's the definition of '*'?

is this pointer?

I've never heard about "pointer" IN RUBY.

Am I wrong?

2009. 01. 08, =BF=C0=C8=C4 8:06, Brian Candler =C0=DB=BC=BA:

> 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 =3D [1,2]
> =3D> [1, 2]
> irb(main):002:0> b =3D [3,4]
> =3D> [3, 4]
> irb(main):003:0> a.insert(a.length, *b)
> =3D> [1, 2, 3, 4]
> irb(main):004:0> a
> =3D> [1, 2, 3, 4]
>
> Of course, a +=3D b is a simpler way to write this.
>
> Do you get something different? I am using ruby-1.8.6p114 under Ubuntu
> Hardy.
> --=20
> Posted via http://www.ruby-....
>
>


Brian Candler

1/8/2009 11:27:00 AM

0

Sebastian Hungerecker 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...
--
Posted via http://www.ruby-....

Brian Candler

1/8/2009 11:34:00 AM

0

Jun Young Kim wrote:
> what's the definition of '*'?

That question was answered yesterday. See
http://www.ruby-...to...
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-...to...

> Am I wrong?

About what? That a.insert(a.length, *b) is somehow funny?
--
Posted via http://www.ruby-....

Sebastian Hungerecker

1/8/2009 11:35:00 AM

0

Jun Young 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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Eustaquio 'TaQ' Rangel

1/8/2009 11:46:00 AM

0

Hi!

2009/1/8 Jun Young Kim <jykim@altibase.com>:
> 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,

? ??

1/8/2009 11:54:00 AM

0

thanks for all your replies.

2009. 01. 08, =EC=98=A4=ED=9B=84 8:46, Eust=C3=A1quio Rangel =EC=9E=91=EC=84=
=B1:

> Hi!
>
> 2009/1/8 Jun Young Kim <jykim@altibase.com>:
>> 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 =3D [1,2]; b =3D [3,4]
> =3D> [3, 4]
> irb(main):002:0> a.push(b)
> =3D> [1, 2, [3, 4]]
> irb(main):003:0> a.push(*b)
> =3D> [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
> =3D> nil
> irb(main):007:0> test(1)
> [1]
> =3D> nil
> irb(main):008:0> test(1,2,3)
> [1, 2, 3]
>
> Regards,
>
>