Multiple Assignments: Newbie question

When I run my program:

arg1="Ruby", arg2="Rails", arg3="Rails"
puts "#{arg1}, #{arg2}, #{arg3}"

Why is the output as follow:
RubyRailsRails, Rails, Rails

What is the reason for this output? Thanks in advance.

Your syntax is incorrect. Either do
arg1="Ruby"; arg2="Rails";arg3="Rails"

or

arg1,arg2,arg3="Ruby","Rails","Rails"

Farrel

The comma is for separating array items. If you were to write:

  arg1 = "Ruby","Rails","Rails"

You'd see that arg1 is an array with those values.

What you want is:

  arg1 ="Ruby"; arg2 = "Rails"; arg3 = "Rails"

Or something along those lines.

V/r
Anthony

···

On 9/5/07, Z T <zoater@gmail.com> wrote:

When I run my program:

arg1="Ruby", arg2="Rails", arg3="Rails"
puts "#{arg1}, #{arg2}, #{arg3}"

Why is the output as follow:
RubyRailsRails, Rails, Rails

What is the reason for this output? Thanks in advance.

--
Cell: 808 782-5046
Cell: 321 505-0025
Current Location: Melbourne, FL

Z T wrote:

When I run my program:

arg1="Ruby", arg2="Rails", arg3="Rails"
puts "#{arg1}, #{arg2}, #{arg3}"

Why is the output as follow:
RubyRailsRails, Rails, Rails

What is the reason for this output? Thanks in advance.

In Ruby x=1,2,3 is the same as x=[1,2,3].
x,y,z=1,2,3 (or x,y,z=[1,2,3]) is the same as x=1; y=2; z=3.
Your code is interpreted as arg1=["Ruby", arg2="Rails", arg3="Rails"]
and since assignments evaluate to the assigned value arg1 now is
["Ruby", "Rails", "Rails"].

HTH,
Sebastian

···

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

Z T wrote:

When I run my program:

arg1="Ruby", arg2="Rails", arg3="Rails"
puts "#{arg1}, #{arg2}, #{arg3}"

Why is the output as follow:
RubyRailsRails, Rails, Rails

What is the reason for this output? Thanks in advance.

The reason is that assignment returns something. So arg3="Rails" actually returns the string "Rails" and as the other posters have pointed out the comma is a way of listing array items. So starting from the right

arg1="Ruby", arg2="Rails", arg3="Rails"

We do the first assignment (arg3="Rails") which gives us

arg1="Ruby", arg2="Rails", "Rails"

We do the second assignment (arg2="Rails") which gives us

arg1="Ruby", "Rails", "Rails"

And finally we do the last assignment (arg1="Ruby", "Rails", "Rails")

Clear as mud :slight_smile:

Anthony, Farrel I am aware of what you are saying. I am trying to figure out
why the code I wrote (purposely) works that way. What is the reason - could
not find any answer anywhere so far? If the way I have written code is
incorrect then why don't I get an error or something?

···

On 9/5/07, Farrel Lifson <farrel.lifson@gmail.com> wrote:

Your syntax is incorrect. Either do
arg1="Ruby"; arg2="Rails";arg3="Rails"

or

arg1,arg2,arg3="Ruby","Rails","Rails"

Farrel

This could be a stupid question -- and if so please forgive me -- but it seems like the output should be

RubyRailsRails, RailsRails, Rails

not

RubyRailsRails, Rails, Rails

So why doesn't arg2 read its right-hand side as an array when arg3 does?

allbests,

....................................................................................................
John Browning

···

On 5 Sep 2007, at 12:55, Peter Hickman wrote:

Z T wrote:

When I run my program:

arg1="Ruby", arg2="Rails", arg3="Rails"
puts "#{arg1}, #{arg2}, #{arg3}"

Why is the output as follow:
RubyRailsRails, Rails, Rails

What is the reason for this output? Thanks in advance.

The reason is that assignment returns something. So arg3="Rails" actually returns the string "Rails" and as the other posters have pointed out the comma is a way of listing array items. So starting from the right

arg1="Ruby", arg2="Rails", arg3="Rails"

We do the first assignment (arg3="Rails") which gives us

arg1="Ruby", arg2="Rails", "Rails"

We do the second assignment (arg2="Rails") which gives us

arg1="Ruby", "Rails", "Rails"

And finally we do the last assignment (arg1="Ruby", "Rails", "Rails")

Clear as mud :slight_smile:

Hi --

···

On Wed, 5 Sep 2007, Z T wrote:

Anthony, Farrel I am aware of what you are saying. I am trying to figure out
why the code I wrote (purposely) works that way. What is the reason - could
not find any answer anywhere so far? If the way I have written code is
incorrect then why don't I get an error or something?

It's not incorrect -- Ruby can run it -- it just doesn't do what you
thought it would.

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

Z T wrote:

> Your syntax is incorrect.

Anthony, Farrel I am aware of what you are saying. I am trying to figure
out why the code I wrote (purposely) works that way.

I hope my previous reply answered that question for you.

If the way I have written code
is incorrect then why don't I get an error or something?

Because it's not. The syntax is valid, it just doesn't do what you expected it
to.

···

On 9/5/07, Farrel Lifson <farrel.lifson@gmail.com> wrote:

--
NP: The Haunted - Privation of Faith Inc
Jabber: sepp2k@jabber.org
ICQ: 205544826

The code is syntactically correct, as other responses to your original
post have explained, it just doesn't behave as you expected.

V/r
Anthony

···

On 9/5/07, Z T <zoater@gmail.com> wrote:

Anthony, Farrel I am aware of what you are saying. I am trying to figure out
why the code I wrote (purposely) works that way. What is the reason - could
not find any answer anywhere so far? If the way I have written code is
incorrect then why don't I get an error or something?

--
Cell: 808 782-5046
Cell: 321 505-0025
Current Location: Melbourne, FL

John Browning wrote:

> Z T wrote:

>> arg1="Ruby", arg2="Rails", arg3="Rails"

So why doesn't arg2 read its right-hand side as an array when arg3
does?

Because it's interpreted as arg1="Ruby", (arg2="Rails"), (arg3="Rails")
not arg1="Ruby", arg2=("Rails", arg3="Rails").

HTH,
Sebastian

···

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

# So why doesn't arg2 read its right-hand side as an array when arg3 does?

many times, if you think ruby, think ruby objects,.. then everything looks so clear...

ruby treats x (like all others) as an object, and the = as the method.
so let that x=1 be x.=(1)

irb(main):073:0> x=y=1
=> 1
irb(main):075:0> x
=> [1]
irb(main):036:0> x=1,2
=> [1, 2]

at this point ruby sees a comma, but ruby assignment treats rhs as a list, ergo array assignment(since there is no better container than array). so the comma rules. thus x=1,2 will be x.=(1,2)

irb(main):041:0> x=y=1,2
=> [1, 2]

wc is x.=(y.=(1),2)
and _not x.=(y.=(1,2))

irb(main):042:0> x
=> [1, 2]
irb(main):043:0> y
=> 1
irb(main):044:0> x=y=1,z=2,3
=> [1, 2, 3]

wc is x.=(y.=(1),z.=(2),3)

irb(main):045:0> x
=> [1, 2, 3]
irb(main):046:0> y
=> 1
irb(main):047:0> z
=> 2
irb(main):066:0> x=y=1,z=[2,a=3],b=c=4

wc is x.=(y.=(1),z.=([2,a.=(3)]),b.=(c.=(r)))

=> [1, [2, 3], 4]
irb(main):067:0> x
=> [1, [2, 3], 4]
irb(main):068:0> y
=> 1
irb(main):069:0> z
=> [2, 3]
irb(main):070:0> b
=> 4
irb(main):071:0> c
=> 4
irb(main):104:0> a
=> 3

you can simulate w ruby, of course.

irb(main):145:0> class Xclass
irb(main):146:1> @x=1
irb(main):147:1> def my=(*other)
irb(main):148:2> @x = *other
irb(main):149:2> end
irb(main):150:1> def showx
irb(main):151:2> @x
irb(main):152:2> end
irb(main):153:1> end
=> nil
irb(main):154:0> x=Xclass.new
=> #<Xclass:0xb7de1a2c>
irb(main):155:0> x.my=1
=> 1
irb(main):156:0> x.showx
=> 1
irb(main):157:0> x.my=1,2
=> [1, 2]
irb(main):158:0> x.showx
=> [1, 2]
irb(main):159:0> x.my=1,y=2
=> [1, 2]
irb(main):160:0> x.showx
=> [1, 2]
irb(main):161:0> y
=> 2
irb(main):162:0> y=nil
=> nil
irb(main):163:0> y=y
=> nil
irb(main):164:0> y=nil
=> nil
irb(main):165:0> x.my=100
=> 100
irb(main):166:0> x.showx
=> 100
irb(main):167:0> x.my=100,200
=> [100, 200]
irb(main):168:0> x.showx
=> [100, 200]
irb(main):169:0> x.my=100,y=200
=> [100, 200]
irb(main):170:0> x.showx
=> [100, 200]
irb(main):171:0> y
=> 200

i forgot about the star (*) op

irb(main):180:0> x=1,2,3
=> [1, 2, 3]
irb(main):181:0> x=[1,2,3]
=> [1, 2, 3]
irb(main):182:0> x=1,[1,2,3]
=> [1, [1, 2, 3]]
irb(main):183:0> x=1,*[1,2,3]
=> [1, 1, 2, 3]

the leading "*" expands the array and converts it to an argument list
so,

irb(main):184:0> x=1,*[1,2,3,[4]]
=> [1, 1, 2, 3, [4]]

is same as

irb(main):188:0> x=1,1,2,3,[4]
=> [1, 1, 2, 3, [4]]

arggh, it's getting longer. i must stop here.
hth.

kind regards -botp

···

From: John Browning [mailto:listguy@poplar.com]

Sebastian and Peter thanks for the clarifications - it's now clear to me.

···

On 9/5/07, Sebastian Hungerecker <sepp2k@googlemail.com> wrote:

Z T wrote:
> On 9/5/07, Farrel Lifson <farrel.lifson@gmail.com> wrote:

> > Your syntax is incorrect.
>
> Anthony, Farrel I am aware of what you are saying. I am trying to figure
> out why the code I wrote (purposely) works that way.

I hope my previous reply answered that question for you.

> If the way I have written code
> is incorrect then why don't I get an error or something?

Because it's not. The syntax is valid, it just doesn't do what you
expected it
to.

--
NP: The Haunted - Privation of Faith Inc
Jabber: sepp2k@jabber.org
ICQ: 205544826

Actually, not really.

In the statement

  x = 1

While 1 is indeed an object, x is a variable. Now after that
statement is executed x will be BOUND to the object 1, but there's no
method involved in evaluating x = 1.

I've seen a lot of confusion in ruby-talk over variables vs. objects.
Note that an assignment may cause a method invocation such as in the
cases:

class A
  attr_accessor :x
end

A.new.x = 1

or:

h = Hash.new(42)

h[5] = 10

The fact that ruby assignments either send a "setter" message or not
depending on the definition of the lhs of the assignment, is an
undercurrent to the recent ruby-talk thread about the semantics of
h[5] ||= 10 given the definition of h above.

That all said just why DOES Ruby interpret

arg1="Ruby", arg2="Rails", arg3="Rails"

as

  arg1 = ("Ruby",(arg2="Rails"),(arg3="Rails")

The answer lies in Ruby's parser rather than with objects and variables.

···

On 9/6/07, Peña, Botp <botp@delmonte-phil.com> wrote:

From: John Browning [mailto:listguy@poplar.com]
# So why doesn't arg2 read its right-hand side as an array when arg3 does?

many times, if you think ruby, think ruby objects,.. then everything looks so clear...

ruby treats x (like all others) as an object, and the = as the method.
so let that x=1 be x.=(1)

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

# arg1 = ("Ruby",(arg2="Rails"),(arg3="Rails")
# The answer lies in Ruby's parser rather than with objects and variables.

i usually think (no worry, my thoughts are usually wrong) assignments are just methods too, so,

   arg1 = "Ruby",arg2="Rails",arg3="Rails"

becomes

   arg1.someassignment( "Ruby",arg2="Rails",arg3="Rails" )

internally becomes

   arg1.someassignment( *("Ruby",arg2="Rails",arg3="Rails") )

internally becomes

   arg1.someassignment( ["Ruby",arg2="Rails",arg3="Rails"] )

wc is easy to understand, rubyish, obj-oriented, no? And i do even have to remember how the parser does it.

kind regards -botp

···

From: Rick DeNatale [mailto:rick.denatale@gmail.com]