From: Julian Leviston Date: 2013-03-29T10:18:28+09:00 Subject: Re: Confusion with setter method On 29/03/2013, at 5:58 AM, Love U Ruby 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 `
' >>> A.new.property=(1,2) > SyntaxError: (irb):10: syntax error, unexpected ',', expecting ')' > A.new.property=(1,2) > ^ > from C:/Ruby200/bin/irb:12:in `
' > ========================================================================== > > 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/. > 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.