[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Dumb question about assigments

Andrew Huh?

7/24/2006 3:07:00 PM

Hi, I feel kind of dumb for asking this, but I can't honestly find the
answer anywere.

I came across a attribute defined like:
class BlahBlah
def something=(x,y)
#code
end
end

I tried to assign the values as:
BlahBlah.something=(5,10)
and
BlahBlah.something=5,10
and
BlahBlah.something(5,10)

but all give me a "1 for 2" argument error (except for the last which
don't work because something is not a method).
All examples I came across either used only one argument, or defined
something as a method (strip the =).

How can I assign values to this tipe of attribute?
thanks

--
Posted via http://www.ruby-....

9 Answers

Tim Pease

7/24/2006 4:07:00 PM

0

Ruby only allows a single value to be assigned using the = operator.
However, you can assign an array to multiple values:

a, b = [1, 2]
puts a #=> 1
puts b #=> 2

When you try to assign multiple values using the = operator you
defined, Ruby is converting the arguments on the right hand side into
an array; hence the "1 for 2" argument error you mentioned. I would
try something like this

class BlahBlah
def something=( *args )
x, y = args
puts x
puts y
end
end

BlahBlah.new.something = 1
BlahBlah.new.something = 1, 2

#=> 1
#=> nil

#=> 1
#=> 2

In the first assignment, y gets the value of nil.

Hope this helps :)

TwP


On 7/24/06, Andrew Huh? <genooma@gmail.com> wrote:
> Hi, I feel kind of dumb for asking this, but I can't honestly find the
> answer anywere.
>
> I came across a attribute defined like:
> class BlahBlah
> def something=(x,y)
> #code
> end
> end
>
> I tried to assign the values as:
> BlahBlah.something=(5,10)
> and
> BlahBlah.something=5,10
> and
> BlahBlah.something(5,10)
>
> but all give me a "1 for 2" argument error (except for the last which
> don't work because something is not a method).
> All examples I came across either used only one argument, or defined
> something as a method (strip the =).
>
> How can I assign values to this tipe of attribute?
> thanks
>
> --
> Posted via http://www.ruby-....
>
>

Andrew Huh?

7/24/2006 4:33:00 PM

0

Tim Pease wrote:
> Ruby only allows a single value to be assigned using the = operator.
> However, you can assign an array to multiple values:
[..]
> Hope this helps :)
>
> TwP

Yep, that certainly helped, I should smack the programmer in the head
then?, perhaps he placed the = by accident.
Thanks!

--
Posted via http://www.ruby-....

Patrick Hurley

7/24/2006 4:36:00 PM

0

On 7/24/06, Andrew Huh? <genooma@gmail.com> wrote:
> I came across a attribute defined like:
> class BlahBlah
> def something=(x,y)
> #code
> end
> end
>
> How can I assign values to this tipe of attribute?
> thanks
>

Well this is pretty questionable code, but if you need to call it try:

b = BlahBlah.new
b.send :something=, x, y

Good luck
pth

Florian Frank

7/24/2006 4:36:00 PM

0

Andrew Huh? wrote:
> I tried to assign the values as:
> BlahBlah.something=(5,10)
> and
> BlahBlah.something=5,10
> and
> BlahBlah.something(5,10)
>
blah = BlahBlah.new
blah.__send__(:something=, 5, 10)

Though it's likely a dumb idea to define =-methods in this way.



Morton Goldberg

7/24/2006 4:41:00 PM

0

That is very strange code you've posted. I wonder what the original
author intended? The only way I could get a two-argument writer
method to run was as follows:

#! /usr/bin/ruby -w

class M

attr_reader :m

def initialize
@m = [0, 0]
end

def m=(x, y)
@m = [x, y]
end

end

foo = M.new
p foo.m => [0, 0]
foo.send(:m=, 1, 2)
p foo.m => [1, 2]

------- end of code --------

The above proves that a two-argument writer can be called (sort of),
but it really doesn't cast any light on what it would be used for. I
hope some wiser head will clarify this.

Regards, Morton

On Jul 24, 2006, at 11:07 AM, Andrew Huh? wrote:

> Hi, I feel kind of dumb for asking this, but I can't honestly find the
> answer anywere.
>
> I came across a attribute defined like:
> class BlahBlah
> def something=(x,y)
> #code
> end
> end
>
> I tried to assign the values as:
> BlahBlah.something=(5,10)
> and
> BlahBlah.something=5,10
> and
> BlahBlah.something(5,10)
>
> but all give me a "1 for 2" argument error (except for the last which
> don't work because something is not a method).
> All examples I came across either used only one argument, or defined
> something as a method (strip the =).
>
> How can I assign values to this tipe of attribute?
> thanks
>
> --
> Posted via http://www.ruby-....
>


Andrew Huh?

7/24/2006 4:50:00 PM

0

Morton Goldberg wrote:
> The above proves that a two-argument writer can be called (sort of),
> but it really doesn't cast any light on what it would be used for. I
> hope some wiser head will clarify this.
>
> Regards, Morton
Now that I know this is not common (I don't have much real life
experience with ruby), I'm sure he placed the = by accident.

--
Posted via http://www.ruby-....

F. Senault

7/25/2006 12:18:00 AM

0

Le 24 juillet 2006 à 18:36, Florian Frank a écrit :

> Though it's likely a dumb idea to define =-methods in this way.

Mind you, in some languages (well, at least - don't shoot ! - Visual
Basic), it allows to type (the equivalent of) blah.something(10) = 5
(first argument is the value assigned, the others passed in the
partenthesis).

Fred
--
When you're brought into this world
They say you're born in sin Well at least they gave me something
I didn't have to steal or have to win Well they tell me that I'm wanted
Yeah, I'm a wanted man (Bon Jovi, Blaze of Glory)

Nobuyoshi Nakada

7/25/2006 7:35:00 AM

0

Hi,

At Tue, 25 Jul 2006 09:20:05 +0900,
F. Senault wrote in [ruby-talk:203659]:
> Mind you, in some languages (well, at least - don't shoot ! - Visual
> Basic), it allows to type (the equivalent of) blah.something(10) = 5
> (first argument is the value assigned, the others passed in the
> partenthesis).

It's tough for ruby parser.

--
Nobu Nakada

F. Senault

7/25/2006 10:10:00 PM

0

Le 25 juillet 2006 à 09:34, nobu@ruby-lang.org a écrit :

> Hi,
>
> At Tue, 25 Jul 2006 09:20:05 +0900,
> F. Senault wrote in [ruby-talk:203659]:
>> Mind you, in some languages (well, at least - don't shoot ! - Visual
>> Basic), it allows to type (the equivalent of) blah.something(10) = 5
>> (first argument is the value assigned, the others passed in the
>> partenthesis).
>
> It's tough for ruby parser.

Sure, ruby already has plenty of constructs for this purpose. I was
just pointing a possible origin of the idiom.

Fred
--
Just a reflection Just a glimpse Just a little reminder Of all the
what abouts And all the might have Could have beens Another day Some
other way But not another reason to continue And now you're one of us
The wretched (Nine Inch Nails, The Wretched)