Splat operator and Ruby instance variable assignments

@ubuntu:~$ irb --simple-prompt

class Foo
def initialize(x,y,z)
@x, @y, @z= x, y,z
end
def to_ary
[@x, @y, @z]
end
def to_hash
[@x => @y]
end
end

=> nil

foo = Foo.new(10,11,12)

=> #<Foo:0x00000001191738 @x=10, @y=11, @z=12>

My Question is with the below :

a,*b = foo

=> #<Foo:0x00000001191738 @x=10, @y=11, @z=12>

a

=> 10

b

=> [11, 12]

How or which internal method has been called to make such assignment to
a,*b from instance variables?

Thanks

···

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

set_trace_func lambda {|*x| p x}

and investigate yourself.

Cheers

robert

···

On Mon, Feb 18, 2013 at 8:38 PM, Love U Ruby <lists@ruby-forum.com> wrote:

@ubuntu:~$ irb --simple-prompt

class Foo
def initialize(x,y,z)
@x, @y, @z= x, y,z
end
def to_ary
[@x, @y, @z]
end
def to_hash
[@x => @y]
end
end

=> nil

foo = Foo.new(10,11,12)

=> #<Foo:0x00000001191738 @x=10, @y=11, @z=12>

My Question is with the below :

a,*b = foo

=> #<Foo:0x00000001191738 @x=10, @y=11, @z=12>

a

=> 10

b

=> [11, 12]

How or which internal method has been called to make such assignment to
a,*b from instance variables?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Now I have tried the below :

C:\>irb --simple-prompt

  class Foo
  def initialize(x,y,z)
  @x, @y, @z= x, y,z
  end
  def to_hash
  [@x => @y]
  end
  end

=> nil

foo=Foo.new(10,11,12)

=> #<Foo:0x11f8968 @x=10, @y=11, @z=12>

a,*b=foo

=> #<Foo:0x11f8968 @x=10, @y=11, @z=12>

a

=> #<Foo:0x11f8968 @x=10, @y=11, @z=12>

b

=>

Here the results are not same as with the above. So is it calling my
first instance method automatically? Couldn't understand the logic.

···

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

Ok, understood what the guy wants ...

"Love U Ruby" is a troll trying to steal time.
See his other "posts".

If his original question would have been solely
about * then he would not have had the need to
write such awful code to pseudo-legitimize his
question.

I mean, he *must* know ruby because he purposely
returns an Array, and calls the method to_hash:

  def to_hash
    [@x => @y]
  end

WTF?!?!

To the guy behind "Love U Ruby", please - go grow
up, find a job, then you won't have a need to go
trolling for reactions.

···

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

This whole exchange reminds of this quote:

I went to a bookstore and asked the saleswoman, "Where's the self-help
section?" She said if she told me, it would defeat the purpose.

Anyway, consider this line of reasoning:
1) Do not feed the trolls.
2) Goto 1

···

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

when we write the "range" with splat(*) generally got the array as
below:

a = *(2..5)

=> [2, 3, 4, 5]

But in the previous case why I got [2, 3, 4, 5] not [[2, 3, 4, 5]]

a = [*(2..5)]

=> [2, 3, 4, 5]

here and there concept in Ruby.... Huuhh :wink:

···

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

Love U Ruby wrote in post #1097729:

Now I have tried the below :

C:\>irb --simple-prompt

  class Foo
  def initialize(x,y,z)
  @x, @y, @z= x, y,z
  end
  def to_hash
  [@x => @y]
  end
  end

=> nil

foo=Foo.new(10,11,12)

=> #<Foo:0x11f8968 @x=10, @y=11, @z=12>

a,*b=foo

=> #<Foo:0x11f8968 @x=10, @y=11, @z=12>

a

=> #<Foo:0x11f8968 @x=10, @y=11, @z=12>

b

=>

Here the results are not same as with the above. So is it calling my
first instance method automatically? Couldn't understand the logic.

WHY do you think that? why you dont get the IDEA that it MAYBE calls
to_ary? becaue *arg is the Splat Operator that parts Arrays into
members, and MAYBE it calls to_ary is the object is not an Array?

···

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

Marc Heiler wrote in post #1097736:

Ok, understood what the guy wants ...

"Love U Ruby" is a troll trying to steal time.
See his other "posts".

Really never thought about to "steal" your time, no intention of that. I
just asked you guys as you are expert. Just give me a tips or trick ...
That's enough, I will dig into that. Please don't tell that way;You are
educated on this, thus asked. And another person also trying to be fit
on your domain, Is it bad? You should be proud by thinking that - what
you love,others too. If anyone incomplete, please try or at-least give
him/her something so that he can be strong.

We should share our knowledge - that's why the forum built,to keep you
and like me, or you like you people come close and help each other.

···

I mean, he *must* know ruby because he purposely
returns an Array, and calls the method to_hash:

  def to_hash
    [@x => @y]
  end

WTF?!?!

To the guy behind "Love U Ruby", please - go grow
up, find a job, then you won't have a need to go
trolling for reactions.

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

This is a legitimate question.

As we've seen, on the splitting side (i.e. the RHS of an assignment, if
any) the splat operator attempts to convert an object into an array, pretty
sure it does it by calling that object's to_a method. Thus the following
would be functionally identical:

  a = *(2..5)
  a = (2..5).to_a

However it does more than that. The splitting side of splat can splice the
splatted value into an array, for example:

  a = [1, *(2..3), 4] # => a = [1,2,3,4]

By paring that down and removing the 1 and 4, you can see why:

  a = [ *(2..3) ] # => a = [2,3]

It splices the splatted range into the existing (anonymous) array. If you
want it to be nested inside, you have to explicitly call `(2..3).to_a`
instead.

···

On 25 February 2013 02:24, Love U Ruby <lists@ruby-forum.com> wrote:

when we write the "range" with splat(*) generally got the array as
below:

>> a = *(2..5)
=> [2, 3, 4, 5]

But in the previous case why I got [2, 3, 4, 5] not [[2, 3, 4, 5]]
>> a = [*(2..5)]
=> [2, 3, 4, 5]
>>

here and there concept in Ruby.... Huuhh :wink:

Marc Heiler wrote in post #1097736:

Ok, understood what the guy wants ...

"Love U Ruby" is a troll trying to steal time.
See his other "posts".

Really never thought about to "steal" your time, no intention of that. I
just asked you guys as you are expert. Just give me a tips or trick ...

Then why do you not work with the replies you get?

We should share our knowledge - that's why the forum built,to keep you
and like me, or you like you people come close and help each other.

Sharing is OK - spoonfeeding is not.

Cheers

robert

···

On Tue, Feb 19, 2013 at 3:46 PM, Love U Ruby <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Yes, the way you do it IS bad. You are a help vampire:

http://slash7.com/2006/12/22/vampires/

As described below, you are a "leech" help vampire:

As I've said before, mend your ways.

···

On Feb 19, 2013, at 6:46, Love U Ruby <lists@ruby-forum.com> wrote:

Really never thought about to "steal" your time, no intention of that. I
just asked you guys as you are expert. Just give me a tips or trick ...
That's enough, I will dig into that. Please don't tell that way;You are
educated on this, thus asked. And another person also trying to be fit
on your domain, Is it bad?

Ryan Davis wrote in post #1097840:

···

On Feb 19, 2013, at 6:46, Love U Ruby <lists@ruby-forum.com> wrote:

Really never thought about to "steal" your time, no intention of that. I
just asked you guys as you are expert. Just give me a tips or trick ...
That's enough, I will dig into that. Please don't tell that way;You are
educated on this, thus asked. And another person also trying to be fit
on your domain, Is it bad?

Yes, the way you do it IS bad. You are a help vampire:

slash7 with Amy Hoy » Blog Archive » Help Vampires: A Spotter’s Guide

As described below, you are a "leech" help vampire:

A Taxonomy of Help Vampires - jasonwryan.com

As I've said before, mend your ways.

It's all about the perception... depends on people mentality. Forum is
to help someone. I would agree with Robert, although not with you.

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

Firstly this is not a forum but a mailing list.
Second we do not come here to help people, we come here because we are
members of a community. We help people because we feel like it not because
we have a duty to.

You are right about one thing though, it is all about perception. Why do
you think people call you help vampire?

···

On 19 February 2013 20:35, Love U Ruby <lists@ruby-forum.com> wrote:

Forum is to help someone.

It is about the perception of the group. Not the individual. The more you behave the way you do, the more the group will perceive you as a help vampire. Tho, after seeing the rest of your responses to this thread, the more you come off like a troll. Keep up the good work.

···

On Feb 19, 2013, at 12:35 , Love U Ruby <lists@ruby-forum.com> wrote:

Ryan Davis wrote in post #1097840:

On Feb 19, 2013, at 6:46, Love U Ruby <lists@ruby-forum.com> wrote:

Really never thought about to "steal" your time, no intention of that. I
just asked you guys as you are expert. Just give me a tips or trick ...
That's enough, I will dig into that. Please don't tell that way;You are
educated on this, thus asked. And another person also trying to be fit
on your domain, Is it bad?

Yes, the way you do it IS bad. You are a help vampire:

slash7 with Amy Hoy » Blog Archive » Help Vampires: A Spotter’s Guide

As described below, you are a "leech" help vampire:

A Taxonomy of Help Vampires - jasonwryan.com

As I've said before, mend your ways.

It's all about the perception... depends on people mentality. Forum is
to help someone. I would agree with Robert, although not with you.

Peter Hickman wrote in post #1097848:

Forum is to help someone.

Firstly this is not a forum but a mailing list.
Second we do not come here to help people, we come here because we are
members of a community. We help people because we feel like it not
because
we have a duty to.

You are right about one thing though, it is all about perception. Why do
you think people call you help vampire?

Leave it. Answering such cheap things seems to me really time wastage.
"And be remember - a just brown tree welcomes people who are giving
water at it's root, but never disrespect who are not."

~~~ First time I am seeing this community such bad mentalities of
people. It seems I am asking money from their accounts. I am asking to
share their tips if any, but ........ have no words.

Thanks

···

On 19 February 2013 20:35, Love U Ruby <lists@ruby-forum.com> wrote:

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

You are asking for their time which is a commodity that can never be got
back. If I spend money I can just earn some more, the time I spend here is
lost when it passes.

I commute 5 hours a day, I work 8 hours a day, I sleep 7 hours a day. Do
the maths

Just replying to this post will take around 10 to 15 minutes as I stop,
rewrite, reedit, reread repeatedly.

Just because we give freely does not mean it is worthless and it is
certainly not cheap.

···

On 19 February 2013 21:22, Love U Ruby <lists@ruby-forum.com> wrote:

~~~ First time I am seeing this community such bad mentalities of
people. It seems I am asking money from their accounts. I am asking to
share their tips if any, but ........ have no words.

Always interesting when people bring out the proverbs

Leave it. Answering such cheap things seems to me really time wastage.
"And be remember - a just brown tree welcomes people who are giving
water at it's root, but never disrespect who are not."

Are you sure you grasp which role you are in the proverb at the moment? The
next paragraph would seem to indicate otherwise.....

~~~ First time I am seeing this community such bad mentalities of
people. It seems I am asking money from their accounts. I am asking to
share their tips if any, but ........ have no words.

Not very respectful of those not "giving water".

Going back to your question - shocking what the words "splat ruby" might
bring someone as a first hit.

John

···

On Tue, Feb 19, 2013 at 1:22 PM, Love U Ruby <lists@ruby-forum.com> wrote:

Peter Hickman wrote in post #1097851:

~~~ First time I am seeing this community such bad mentalities of
people. It seems I am asking money from their accounts. I am asking to
share their tips if any, but ........ have no words.

You are asking for their time which is a commodity that can never be got
back. If I spend money I can just earn some more, the time I spend here
is

These all are vague.

Then how should I call "Google" who keep sharing the knowledge to
everyone "FREE", how would you consider "YouTube" who does the same job.

How would you consider the the man : "David A. Black" writer of "The
Well-Grounded Ruby". Go to the link: and see how is he redelivering all
his speech what he already mentioned in his book and "YouTube" giving us
it FREE : http://www.youtube.com/watch?v=Xt6c6r4fbBs

So in simple sentence it is depends upon people mentality, who felt
disturbed when he shouldn't but he can obviously. But in such a case
don't need to be keep shouting. If you love to share knowledge share
it,otherwise keep quite. Don't try to assemble a group of like you.

Its not about help,not about money, only about dedications... then you
don't have. --- Thanks

···

On 19 February 2013 21:22, Love U Ruby <lists@ruby-forum.com> wrote:

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

John W Higgins wrote in post #1097852:

Always interesting when people bring out the proverbs

~~~ First time I am seeing this community such bad mentalities of
people. It seems I am asking money from their accounts. I am asking to
share their tips if any, but ........ have no words.

Not very respectful of those not "giving water".

Your catch is wrong, so I am helpless.

···

On Tue, Feb 19, 2013 at 1:22 PM, Love U Ruby <lists@ruby-forum.com> > wrote:

John

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

You could also finally shut up, as it wastes the time of every list subscriber to scan and delete your messages every day.

···

On Tue, 19 Feb 2013 22:37:59 +0100, Peter Hickman <peterhickman386@googlemail.com> wrote:

Just replying to this post will take around 10 to 15 minutes as I stop,
rewrite, reedit, reread repeatedly.

--
Matma Rex