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

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

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:

object.namer=("NAME","SURNAME")

SyntaxError: compile error
(irb):34: syntax error, unexpected ',', expecting ')'

But you can use "send":

···

El Domingo, 13 de Julio de 2008, Luiz Vitor Martinez Cardoso escribió:

object.send("namer=","NAME","SURNAME")

--
Iñaki Baz Castillo

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'

···

On Mon, Jul 14, 2008 at 4:18 AM, Luiz Vitor Martinez Cardoso <grabber@gmail.com> wrote:

Thanks for your attention!

Iñaki Baz Castillo 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:

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

But you can use "send":

> 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

···

El Domingo, 13 de Julio de 2008, Luiz Vitor Martinez Cardoso escribió:

--
Posted via http://www.ruby-forum.com/\.

Hi --

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

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:

> 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":

> 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, 14 Jul 2008, Iñaki Baz Castillo wrote:

El Domingo, 13 de Julio de 2008, Luiz Vitor Martinez Cardoso escribió:

--
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.rubypal.com for details and updates!

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]

···

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

Ryan Davis 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"

···

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

--
Posted via http://www.ruby-forum.com/\.

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.rubypal.com for details and updates!

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.rubypal.com for details and updates!