Passing arguments to a class, how do?

  • Thread starter Luiz Vitor Martinez Cardoso
  • Start date
L

Luiz Vitor Martinez Cardoso

[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
 
I

Iñaki Baz Castillo

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
 
S

Siep Korteling

Iñaki Baz Castillo said:
El Domingo, 13 de Julio de 2008, Luiz Vitor Martinez Cardoso escribió:

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
 
M

Michael Fellinger

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'
 
B

black eyes

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

x=(1,2)
well...
=> [1, 2]

I think in ruby now, can't use = for multi-paremeters, which is for
future version of ruby
=> [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"
 
D

David A. Black

Hi --

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

x=(1,2)
well...
=> [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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top