Confusion with setter method

C:\>irb --simple-prompt
DL is deprecated, please use Fiddle

class A
  def property= value
    x, y = value
    p value
end
end

=> nil

A.new.property= 1,2

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

A.new.property = 1,2

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

A.new.property =(1,2)

SyntaxError: (irb):9: syntax error, unexpected ',', expecting ')'
A.new.property =(1,2)
                   ^
        from C:/Ruby200/bin/irb:12:in `<main>'

A.new.property=(1,2)

SyntaxError: (irb):10: syntax error, unexpected ',', expecting ')'
A.new.property=(1,2)
                  ^
        from C:/Ruby200/bin/irb:12:in `<main>'

···

==========================================================================

I am on below version:

C:\>ruby -v
ruby 2.0.0p0 (2013-02-24) [i386-mingw32]
***************************************

Questions:

(a) How does without `splat` operator value took 1,2?
(b) Why did `A.new.property= 1,2` work well,but `A.new.property= (1,2)`
not?

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

Because [1,2] is one argument, and (1,2) is two arguments.

···

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

Ruby syntax can be relaxed. This is a good and bad thing. It's powerful, but it requires that you know what you're doing.

Ruby can only interpret A.new.property=(1,2) as a call to a method "property=" that has two arguments, but you have no such method. Your method only takes *one* argument. So, you get a syntax error.

However, Ruby can interpret A.new.property= 1,2 as either a call to a method with one or two arguments, so it tries to do the right thing.
The more explicit version of *this* call is A.new.property= [1,2] or for the most explicit version: A.new.property=([1,2])

Intent is incredibly important, so it's good to explain your intent in each case when you write code you want explanations for. Just putting code up won't signal your intent in each case, *or* what you think is going to happen. It's good to note both when asking for help.

···

On 29/03/2013, at 5:58 AM, Love U Ruby <lists@ruby-forum.com> wrote:

C:\>irb --simple-prompt
DL is deprecated, please use Fiddle

class A
def property= value
   x, y = value
   p value
end
end

=> nil

A.new.property= 1,2

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

A.new.property = 1,2

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

A.new.property =(1,2)

SyntaxError: (irb):9: syntax error, unexpected ',', expecting ')'
A.new.property =(1,2)
                  ^
       from C:/Ruby200/bin/irb:12:in `<main>'

A.new.property=(1,2)

SyntaxError: (irb):10: syntax error, unexpected ',', expecting ')'
A.new.property=(1,2)
                 ^
       from C:/Ruby200/bin/irb:12:in `<main>'

I am on below version:

C:\>ruby -v
ruby 2.0.0p0 (2013-02-24) [i386-mingw32]
***************************************

Questions:

(a) How does without `splat` operator value took 1,2?
(b) Why did `A.new.property= 1,2` work well,but `A.new.property= (1,2)`
not?

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

That's not the reason.

···

On 29/03/2013, at 6:54 AM, Joel Pearson <lists@ruby-forum.com> wrote:

Because [1,2] is one argument, and (1,2) is two arguments.

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

Julian Leviston wrote in post #1103593:

···

On 29/03/2013, at 5:58 AM, Love U Ruby <lists@ruby-forum.com> wrote:

Ruby syntax can be relaxed. This is a good and bad thing. It's powerful,
but it requires that you know what you're doing.

Ruby can only interpret A.new.property=(1,2) as a call to a method
"property=" that has two arguments, but you have no such method. Your
method only takes *one* argument. So, you get a syntax error.

However, Ruby can interpret A.new.property= 1,2 as either a call to a
method with one or two arguments, so it tries to do the right thing.
The more explicit version of *this* call is A.new.property= [1,2] or for
the most explicit version: A.new.property=([1,2])

Nice explanation,I understood. :slight_smile:

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

I'd suggest you explain that, because that IS the reason, based on the
shown code. If you disagree, you'll have to do more than say its not the
reason and not say anything else.

···

On Mar 28, 2013 9:10 PM, "Julian Leviston" <julian@coretech.net.au> wrote:

That's not the reason.

On 29/03/2013, at 6:54 AM, Joel Pearson <lists@ruby-forum.com> wrote:

> Because [1,2] is one argument, and (1,2) is two arguments.
>
> --
> Posted via http://www.ruby-forum.com/\.
>