[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Multiple Assignments: Newbie question

Peter Hickman

9/5/2007 11:56:00 AM

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


5 Answers

Sebastian Hungerecker

9/5/2007 1:15:00 PM

0

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

Peña, Botp

9/6/2007 4:29:00 AM

0

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)

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


Rick DeNatale

9/6/2007 3:58:00 PM

0

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)

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.
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Peña, Botp

9/7/2007 1:03:00 AM

0

From: Rick DeNatale [mailto:rick.denatale@gmail.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

hotac

2/24/2011 2:18:00 PM

0

On Feb 24, 8:56 am, ":))" <bennypo...@gmail.com> wrote:
> >“Ngày 1-11-1963 rút l?i ch? còn là m?t ngày d?o chính m? ra m?t giai
> >do?n l?ch s? d?y h?n lo?n v?i bao nhiêu là b?p bênh: h?t ch?nh lý l?i
> >d?n d?o chánh, quy?n hành chuy?n t? tay n? qua tay kia và xã h?i càng
> >ngày càng thi?u ?n d?nh.
>
> ---------------------------------------------------------------------------­---------------------------------------------------------
> Chie^'n tranh la.nh giu*~a Hoa-Ky` va` kho^'i Co^.ng Sa?n gia ta(ng
> tre6n toa`n ca^`u va` co' a?nh hu*o*?ng dde^'n ti`nh hi`nh Vietnam
> chu*' cha(?ng pha?i sau khi o^ng Die^.m & Nhu bi. sa't ha.i thi`
> chie^'n tranh ta.i VN leo thang gi` ca?.
>
> Hoan Ho^ HDQNCM (ho^.i ddo^`ng qua^n nha^n ca'ch ma.ng) va` QLVNCH
> dda~ he^'t ta^m ba?o ve^. mie^`n Nam thoa't kho?i go^ng cu`m Co^.ng Sa?
> n va` che^' ddo^. ddo^.c ta`i gia ddi`nh tri. cu?a Ngo^ DDi`nh Die^.m,
> Ngo^ DDi`nh Nhu, Ngo^ DDi`nh Thu.c, v.v...
>
> Hip hip Hourra !  Hip hip Hourra...Hoan Ho^ !
>
> ;-)))

B?n tr? c?: Hoan hô HÐQNCM!

Lyndon B. Johnson: Oh! The gang of thugs == b?n côn d?


B?n tr? c?: Hoan hô TT Trích Trí Quang dã BBQ: 8 su sãi th?i ông
Di?m,
20 su sãi th?i ông K?!


Lyndon B. Johnson: Oh! Thôi b? di tám ba cái th?m k?ch nh?m nhí ?y !