[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Passing arguments to a class, how do?

Luiz Vitor Martinez Cardoso

7/13/2008 7:18:00 PM

[Note: parts of this message were removed to make it a legal post.]

I'm learning Ruby, and i need to solve a problem:


class Person
def namer=(name, surname)
@var = name
@car = surname
end
end

object = Person.new
object.namer = (how to pass the params here) ?

Thanks for your attention!

--
Regards,

Luiz Vitor Martinez Cardoso
cel.: (11) 8187-8662
blog: rubz.org
engineer student at maua.br

7 Answers

Iñaki Baz Castillo

7/13/2008 8:16:00 PM

0

El Domingo, 13 de Julio de 2008, Luiz Vitor Martinez Cardoso escribi=C3=B3:
> I'm learning Ruby, and i need to solve a problem:
>
>
> class Person
> def namer=3D(name, surname)
> @var =3D name
> @car =3D surname
> end
> end
>
> object =3D Person.new
> object.namer =3D (how to pass the params here) ?

I don't know why but it's not possible to define an instance method sufixed=
=20
with "=3D" and requiring more than one parameter, it gives an error when=20
passing parameters:

irb> object.namer=3D("NAME","SURNAME")
SyntaxError: compile error
(irb):34: syntax error, unexpected ',', expecting ')'

But you can use "send":

irb> object.send("namer=3D","NAME","SURNAME")







=2D-=20
I=C3=B1aki Baz Castillo

Siep Korteling

7/13/2008 8:33:00 PM

0

Iñaki Baz Castillo wrote:
> El Domingo, 13 de Julio de 2008, Luiz Vitor Martinez Cardoso escribió:
>> object = Person.new
>> object.namer = (how to pass the params here) ?
>
> I don't know why but it's not possible to define an instance method
> sufixed
> with "=" and requiring more than one parameter, it gives an error when
> passing parameters:
>
> irb> object.namer=("NAME","SURNAME")
> SyntaxError: compile error
> (irb):34: syntax error, unexpected ',', expecting ')'
>
> But you can use "send":
>
> irb> object.send("namer=","NAME","SURNAME")

So maybe the OP is happy with dropping the troublesome "=" in his method
name.

class Person
def namer(name, surname)
@var = name
@car = surname
end
end

object = Person.new
object.namer("Boop","Betty")
p object

hth

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

Michael Fellinger

7/13/2008 9:47:00 PM

0

On Mon, Jul 14, 2008 at 4:18 AM, Luiz Vitor Martinez Cardoso
<grabber@gmail.com> wrote:
> I'm learning Ruby, and i need to solve a problem:
>
>
> class Person
> def namer=(name, surname)
> @var = name
> @car = surname
> end
> end
>
> object = Person.new
> object.namer = (how to pass the params here) ?

Usually it's better to do this via initialize

class Person
def initialize(name, surname)
@name, @surname = name, surname
end
end

Person.new('Max', 'Mustermann')

You can combine that with attr_accessor

class Person
attr_accessor :name, :surname

def initialize(name = nil, surname = nil
@name, @surname = name, surname
end
end

person = Person.new('Max')
person.surname = 'Mustermann'

> Thanks for your attention!

Ryan Davis

7/14/2008 12:14:00 AM

0


On Jul 13, 2008, at 16:40 , David A. Black wrote:

> The idea of the =-methods is that they use assignment syntax, and you
> can't do this:
>
> x=(1,2)

well...

>> x = 1, 2
=> [1, 2]


black eyes

7/14/2008 4:37:00 AM

0

Ryan Davis wrote:
> On Jul 13, 2008, at 16:40 , David A. Black wrote:
>
>> The idea of the =-methods is that they use assignment syntax, and you
>> can't do this:
>>
>> x=(1,2)
>
> well...
>
> >> x = 1, 2
> => [1, 2]

I think in ruby now, can't use = for multi-paremeters, which is for
future version of ruby
> >> x = 1, 2
> => [1, 2]
the [1, 2] are also only one parameter

def namer=(args)
p args
end

this also is done!

infact i got the warning "test.rb:9: warning: parenthesize argument(s)
for future version"

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

David A. Black

7/14/2008 11:08:00 AM

0

Hi --

On Mon, 14 Jul 2008, Ryan Davis wrote:

>
> On Jul 13, 2008, at 16:40 , David A. Black wrote:
>
>> The idea of the =-methods is that they use assignment syntax, and you
>> can't do this:
>>
>> x=(1,2)
>
> well...
>
>>> x = 1, 2
> => [1, 2]

That was my point: the =-methods have assignment semantics,
so you can't use parentheses around the arguments because in
assignment semantics that doesn't work.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!

David A. Black

7/14/2008 11:15:00 AM

0

Hi --

On Mon, 14 Jul 2008, black eyes wrote:

> I think in ruby now, can't use = for multi-paremeters, which is for
> future version of ruby
>> >> x = 1, 2
>> => [1, 2]
> the [1, 2] are also only one parameter

You can do:

x,y = 1,2

to assign both, or

x, = 1,2

to discard all the ones after x.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!