# A question on method name

**URL:** <https://www.ruby-forum.com/t/a-question-on-method-name/180126>\
**Category:** Ruby\
**Created:** [December 12, 2009, 8:28am UTC](https://www.ruby-forum.com/t/a-question-on-method-name/180126 "2009-12-12T08:28:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 12, 2009, 8:28am UTC](https://www.ruby-forum.com/t/a-question-on-method-name/180126/1 "2009-12-12T08:28:38Z")

</div>

Hello,

This works fine for me:

class Horse

```
  def name
          @name
  end

    def nameset(v1,v2)
            @name = v1 + v2.to_s
    end

```

end

xx=Horse.new  
xx.nameset("hello ",1)  
p xx.name

But if I changed the method name, it won’t work.

class Horse

```
  def name
          @name
  end

    def name=(v1,v2)
            @name = v1 + v2.to_s
    end

```

end

xx=Horse.new  
xx.name=("hello ",1)  
p xx.name

the error says:

syntax error, unexpected ‘,’, expecting ‘)’  
xx.name=("hello ",1)  
^

Please help, thanks in advance.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 12, 2009, 10:36am UTC](https://www.ruby-forum.com/t/a-question-on-method-name/180126/2 "2009-12-12T10:36:13Z")

</div>

On 12.12.2009 08:27, Ruby N. wrote:

> ```
> end
> 
> ```
> 
> syntax error, unexpected ‘,’, expecting ‘)’  
> xx.name=("hello ",1)  
> ^
> 
> Please help, thanks in advance.

Methods with assignment operator are separately treated by the parser -  
as you just learned. You can only have 1 argument.

Btw, usually an assignment method, does just that. In your case you are  
concatenating two strings which is probably worth a different method  
name.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 12, 2009, 11:18am UTC](https://www.ruby-forum.com/t/a-question-on-method-name/180126/3 "2009-12-12T11:18:33Z")

</div>

Ruby N. wrote:

> Hello,
> 
> This works fine for me:
> 
> class Horse
> 
> ```
> def name
> @name
> end
> 
> def nameset(v1,v2)
> @name = v1 + v2.to_s
> end
> 
> ```
> 
> end
> 
> xx=Horse.new  
> xx.nameset("hello ",1)  
> p xx.name
> 
> But if I changed the method name, it won’t work.
> 
> class Horse
> 
> ```
> def name
> @name
> end
> 
> def name=(v1,v2)
> @name = v1 + v2.to_s
> end
> 
> ```
> 
> end
> 
> xx=Horse.new  
> xx.name=("hello ",1)  
> p xx.name
> 
> the error says:
> 
> syntax error, unexpected ‘,’, expecting ‘)’  
> xx.name=("hello ",1)  
> ^
> 
> Please help, thanks in advance.

Hi,

class Foo

def bar  
@bar  
end

def bar= \*args  
@bar = args.join  
end

end

x = Foo.new.bar= 1,2,3 # =\> [1,2,3]  
x.bar # =\> “123”

- Rob

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [December 12, 2009, 11:23am UTC](https://www.ruby-forum.com/t/a-question-on-method-name/180126/4 "2009-12-12T11:23:21Z")

</div>

> x = Foo.new.bar= 1,2,3 # =\> [1,2,3]  
> x.bar # =\> “123”

Oops. If you want that to work as-is…  
x = Foo.new  
x.bar= 1,2,3 # =\> [1,2,3]  
x.bar # =\> “123”

- Rob
