[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Where would I look to understand why ... works

Jacob, Raymond A Jr

1/5/2007 10:32:00 PM

Thank you,
raymond

-----Original Message-----
From: Jan Svitok [mailto:jan.svitok@gmail.com]
Sent: Friday, January 05, 2007 17:13
To: ruby-talk ML
Subject: Re: Where would I look to understand why ... works

On 1/5/07, Jacob, Raymond A Jr <raymond.jacob@navy.mil> wrote:
>
> I create a small method
>
> def counting(p1,p2)
> p1 = p1 + 1
> p2 = p2 + 2
> return p1,p2
> end

A bit offtopic: you can shorten this to:

def counting(p1,p2)
p1 += 1 # x += y is the same as x = x + y
p2 += 2
return p1,p2
end

or

or even

def counting(p1,p2)
return p1 + 1, p2 + 2
end

> sum-by-1s=0
> sum-by-2s=0
> while line = gets
> # need driver test the method
> line=line.chop
> sum-by-1s, sum-by-2s = counting(sum-by-1s,sum-by-2s) end

I don't think sum-by-1s is a good variable name. You can't use - in a
name. It's reserved for minus. Names are made of A..Z, a..z, 0..9, @ and
_.

> The method works. I am using PickAxe as my reference.
> I don't recall see any thing stating you could assign values in an
> array to variables by separating the variables by commas on the
> leftside of the assignment statement. I just want to know which book
> or reference should I use to understand why assignments can be made
> in this way?
> Or am I just seeing a side effect of my poor coding skills?

This is normal. See the link below. It's used in block parameters as
well

i.e.: you can do [[1,2],[3,4]].each {|a| ... } or [[1,2],[3,4]].each
{|a,b| ... } in the former case you'll get [1,2] and [3,4] for a, in the
latter
a=1,3 and b=2,4.

http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressio...