Passing arguments to a class, how do?

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 C.
cel.: (11) 8187-8662
blog: rubz.org
engineer student at maua.br

El Domingo, 13 de Julio de 2008, Luiz Vitor Martinez C. 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”)

Iñaki Baz C. wrote:

El Domingo, 13 de Julio de 2008, Luiz Vitor Martinez C. 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

Hi –

On Mon, 14 Jul 2008, Iñaki Baz C. wrote:

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 ‘)’

The idea of the =-methods is that they use assignment syntax, and you
can’t do this:

x=(1,2)

But you can use “send”:

irb> object.send(“namer=”,“NAME”,“SURNAME”)

That kind of eliminates the nice assignment syntax :slight_smile:

Try this:

def namer=(args)
p args
end

object.namer = “David”, “Black” # => [“David”, “Black”]

David

On Mon, Jul 14, 2008 at 4:18 AM, Luiz Vitor Martinez C.
[email protected] wrote:

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’

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]

Ryan D. 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”

Hi –

On Mon, 14 Jul 2008, Ryan D. wrote:

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

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