Creating a setter method that takes extra arguments in Ruby

半城伤御伤魂 提交于 2019-12-18 05:34:50

问题


I'm trying to write a method that acts as a setter and takes some extra arguments besides the assigned value. Silly example:

class WordGenerator
  def []=(letter, position, allowed)
    puts "#{letter}#{allowed ? ' now' : ' no longer'} allowed at #{position}"
  end

  def allow=(letter, position, allowed)
    # ...
  end
end

Writing it as an indexer works and I can call it like this:

gen = WordGenerator.new

gen['a', 1] = true
# or explicitly:
gen.[]=('a', 1, true)

But when I try any of the following, the interpreter complains:

gen.allow('a', 1) = false # syntax error
gen.allow=('a', 1, false) # syntax error

Why won't this work, am I missing the obvious?


回答1:


It doesn't work because the parser doesn't allow it. An equals sign is allowed in expressions of the form identifier = expression, expression.identifier = expression (where identifier is \w+), expression[arguments] = expression and expression.[]= arguments and as part of a string or symbol or character literal (?=). That's it.

gen.send(:allow=, 'a', 1, false) would work, but at that point you could as well just give the method a name that doesn't include a =.




回答2:


I have come across this and decided to pass my arguments as an array or hash.

E.g.:

def allow=(arguments)
  puts arguments[:letter]
  puts arguments[:position]
  puts arguments[:allowed]
end

object.allow={:letter=>'A',:position=>3,:allowed=>true}


来源:https://stackoverflow.com/questions/2380796/creating-a-setter-method-that-takes-extra-arguments-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!