[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Where would I look to understand why ... works

Jacob, Raymond A Jr

1/5/2007 9:17:00 PM


I create a small method

def counting(p1,p2)
p1 = p1 + 1
p2 = p2 + 2
return p1,p2
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

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?

Thank you,
Raymond


5 Answers

Rimantas Liubertas

1/5/2007 10:12:00 PM

0

> 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?

I guess you are seeing the effect of not reading the book carefully enough.
PickAxe, 2nd edition (paper version):

Page 91 'Assignment'
Page 92 'Parallel Assignment'
Page 338 'More on Assignment'
Page 340 'Parallel Assignment'


--
Regards,
Rimantas
--
http://rim...

Jano Svitok

1/5/2007 10:13:00 PM

0

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

Ryan Davis

1/6/2007 9:49:00 AM

0


On Jan 5, 2007, at 1:17 PM, Jacob, Raymond A Jr wrote:

>
> I create a small method [...]

Please don't hijack threads. Start a new one instead of replying to
something completely unrelated.


Carlos

1/6/2007 2:50:00 PM

0

[Jan Svitok <jan.svitok@gmail.com>, 2007-01-05 23.12 CET]
[...]
> 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 _.

...unless you set $KCODE. Then ¿almost? everything above \177 is allowed.

$ ruby -Ku -e 'sum�by�1s, sum by 2s = 1,2; p sum�by�1s, sum by 2s'
1
2

--

6127 Dead, 1270 since 1/20/09

5/15/2013 9:32:00 PM

0

On Wed, 15 May 2013 15:29:30 -0600, deep wrote:

> On Wed, 15 May 2013 19:46:33 +0000 (UTC), Zepp <dead@gone.com> wrote:
>
>>On Wed, 15 May 2013 12:21:39 -0700, Klaus Schadenfreude wrote:
>>
>>> On Wed, 15 May 2013 15:21:48 +0000 (UTC), Zepp <dead@gone.com> wrote:
>>>
>>>>On Wed, 15 May 2013 05:09:29 -0700, Klaus Schadenfreude wrote:
>>>>
>>>>> On Tue, 14 May 2013 21:54:23 +0000 (UTC), Zepp <dead@gone.com>
>>>>> wrote:
>>>>>
>>>>>>On Tue, 14 May 2013 12:00:48 -0700, Klaus Schadenfreude wrote:
>>>>>>
>>>>>>> On Tue, 14 May 2013 18:52:42 +0000 (UTC), Zepp <dead@gone.com>
>>>>>>> wrote:
>>>>>>>
>>>>>>>>Granted, the mainstream hates the teabaggers far more than
>>>>>>>>Democrats do.
>>>>>>>
>>>>>>> Thanks for the admission that Democrats aren't "mainstream." We
>>>>>>> always knew you were the lunatic fringe.
>>>>>>
>>>>>>Well, I meant to say [..]
>>>>>
>>>>> Stop posting stoned. That will help.
>>>>
>>>>Are you projecting again, Klod?
>>>
>>> The only other possible explanation for your drivel is that you're
>>> really, really stupid.
>>>
>>> I'll go with that, then.
>>
>>Whatever gets you through the night, Klod.
>
> Don't waste your time with Mr. "Delights in the suffering of others"
> (Schadenfreude). He is a hate freak and will never make an informed
> objective argument. All he wants to do is spew verbal abuse to make
> himself feel like a big man he can never be. You'd be best off ignoring
> him like I do.

Oh, I remember him well. He's a piece of shit. But it's fun to poke him
with a stick from time to time.