0

目前我有这个代码:

name, type = meth.to_s.match(/^(.+?)([=?]?)$/)[1..-1]

但这似乎不是最好的解决方案=\

任何想法如何使它更好?谢谢。

4

2 回答 2

1

最好的选择似乎是这样的: name, type = meth.to_s.split(/([?=])/)

于 2010-09-18T16:39:59.167 回答
0

这大致是我如何实现我的method_missing

def method_missing(sym, *args, &block)
  name = sym.to_s
  if name =~ /^(.*)=$/
    # Setter method with name $1.
  elsif name =~ /^(.*)\?$/
    # Predicate method with name $1.
  elsif name =~ /^(.*)!$/
    # Dangerous method with name $1.
  else
    # Getter or regular method with name $1.
  end
end

或者这个版本,它只计算一个正则表达式:

def method_missing(sym, *args, &block)
  name = sym.to_s
  if name =~ /^(.*)([=?!])$/
    # Special method with name $1 and suffix $2.
  else
    # Getter or regular method with name.
  end
end
于 2010-09-18T22:43:46.887 回答