[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Beginner Question on Ruby

Arun Kumar

5/2/2008 11:52:00 AM

[Note: parts of this message were removed to make it a legal post.]

Hi all,

I am beginning to learn ruby and cant seem to reason this behaviour. I
expect since empty or nill is the array, arr*n shouldnt change it.

Why is this behaviour so?

Code:

num_arr = Array.new
puts "Num Alpha: #{num_arr} and size: #{num_arr.size}"
puts "Num Alpha: #{num_arr << num_arr} and size: #{num_arr.size}"
num_arr.insert(-1, num_arr << num_arr)
puts "Num Alpha: #{num_arr} and size: #{num_arr.size}"

Output:

Num Alpha: and size: 0
Num Alpha: [...] and size: 1
Num Alpha: [...][...][...][...][...][...][...][...][...] and size: 3

5 Answers

David A. Black

5/2/2008 11:57:00 AM

0

Hi --

On Fri, 2 May 2008, Arun Kumar wrote:

> Hi all,
>
> I am beginning to learn ruby and cant seem to reason this behaviour. I
> expect since empty or nill is the array, arr*n shouldnt change it.
>
> Why is this behaviour so?
>
> Code:
>
> num_arr = Array.new
> puts "Num Alpha: #{num_arr} and size: #{num_arr.size}"
> puts "Num Alpha: #{num_arr << num_arr} and size: #{num_arr.size}"
> num_arr.insert(-1, num_arr << num_arr)
> puts "Num Alpha: #{num_arr} and size: #{num_arr.size}"
>
> Output:
>
> Num Alpha: and size: 0
> Num Alpha: [...] and size: 1
> Num Alpha: [...][...][...][...][...][...][...][...][...] and size: 3

You're inserting an array into itself. Ruby represents that kind of
recursive inclusion with dots.

irb(main):005:0> a = []
=> []
irb(main):006:0> a << a
=> [[...]]


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Arun Kumar

5/2/2008 12:16:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Hello David,

Thanks for the answer.

But would you know why it is so? Is there something syntactic or semantic
issue that I am making,

the reason i ask this is...

n = 10
x += n
does a perfect x = nil + n => n initialization, which is good and
explainable.

In the array case however,
One would expect [atleast i am :) ] the elements of the source array [if
any] be appended to the target. Now why is the reference going in making it
a recursive :?

sorry if this question is lame or bozo but i feel a clear behaviour enables
clear understanding or would require even more clearer reasoning.

Cheers
Arun

On Fri, May 2, 2008 at 5:27 PM, David A. Black <dblack@rubypal.com> wrote:

> Hi --
>
>
> On Fri, 2 May 2008, Arun Kumar wrote:
>
> Hi all,
> >
> > I am beginning to learn ruby and cant seem to reason this behaviour. I
> > expect since empty or nill is the array, arr*n shouldnt change it.
> >
> > Why is this behaviour so?
> >
> > Code:
> >
> > num_arr = Array.new
> > puts "Num Alpha: #{num_arr} and size: #{num_arr.size}"
> > puts "Num Alpha: #{num_arr << num_arr} and size: #{num_arr.size}"
> > num_arr.insert(-1, num_arr << num_arr)
> > puts "Num Alpha: #{num_arr} and size: #{num_arr.size}"
> >
> > Output:
> >
> > Num Alpha: and size: 0
> > Num Alpha: [...] and size: 1
> > Num Alpha: [...][...][...][...][...][...][...][...][...] and size: 3
> >
>
> You're inserting an array into itself. Ruby represents that kind of
> recursive inclusion with dots.
>
> irb(main):005:0> a = []
> => []
> irb(main):006:0> a << a
> => [[...]]
>
>
> David
>
> --
> Rails training from David A. Black and Ruby Power and Light:
> INTRO TO RAILS June 9-12 Berlin
> ADVANCING WITH RAILS June 16-19 Berlin
> INTRO TO RAILS June 24-27 London (Skills Matter)
> See http://www.r... for details and updates!
>
>

David A. Black

5/2/2008 12:24:00 PM

0

Hi --

On Fri, 2 May 2008, Arun Kumar wrote:

> On Fri, May 2, 2008 at 5:27 PM, David A. Black <dblack@rubypal.com> wrote:
>
>> Hi --
>>
>>
>> On Fri, 2 May 2008, Arun Kumar wrote:
>>
>> Hi all,
>>>
>>> I am beginning to learn ruby and cant seem to reason this behaviour. I
>>> expect since empty or nill is the array, arr*n shouldnt change it.
>>>
>>> Why is this behaviour so?
>>>
>>> Code:
>>>
>>> num_arr = Array.new
>>> puts "Num Alpha: #{num_arr} and size: #{num_arr.size}"
>>> puts "Num Alpha: #{num_arr << num_arr} and size: #{num_arr.size}"
>>> num_arr.insert(-1, num_arr << num_arr)
>>> puts "Num Alpha: #{num_arr} and size: #{num_arr.size}"
>>>
>>> Output:
>>>
>>> Num Alpha: and size: 0
>>> Num Alpha: [...] and size: 1
>>> Num Alpha: [...][...][...][...][...][...][...][...][...] and size: 3
>>>
>>
>> You're inserting an array into itself. Ruby represents that kind of
>> recursive inclusion with dots.
>>
>> irb(main):005:0> a = []
>> => []
>> irb(main):006:0> a << a
>> => [[...]]
>>
> Hello David,
>
> Thanks for the answer.
>
> But would you know why it is so? Is there something syntactic or semantic
> issue that I am making,
>
> the reason i ask this is...
>
> n = 10
> x += n
> does a perfect x = nil + n => n initialization, which is good and
> explainable.

You can't add 10 to nil. You'll get an error if you try to do that,
because nil has no + method.

> In the array case however,
> One would expect [atleast i am :) ] the elements of the source array [if
> any] be appended to the target. Now why is the reference going in making it
> a recursive :?
>
> sorry if this question is lame or bozo but i feel a clear behaviour enables
> clear understanding or would require even more clearer reasoning.

You've using the << method:

num_arr = Array.new
num_arr << num_arr

which appends the object num_arr (not its elements, but it, itself) to
the object num_arr.

If you want to add the elements of an array to another array, you can
use concat:

irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a.concat(a)
=> [1, 2, 3, 1, 2, 3]

It's all perfectly reasonable -- you just have to know what the
methods you're using actually do :-)


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Arun Kumar

5/2/2008 12:28:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

And now i do :) thanks.

smorgas

10/28/2009 12:58:00 PM

0

On Tue, 27 Oct 2009 15:55:24 -0700, Klaus Schadenfreude
<klausschadenfreude@yahoo.com> wrote:

>>The only "freak(y) thing is you attempting to use the
>>term "left" and "Socialism" as many times as you can--
>
>How many times have I used "socialist?" Go ahead and count. Take off
>your socks if necessary.

How many times you ever been right?

You're a fucking loonytarian that has NO historical
basis of success in anything.