現時点で私はこのコードを持っています:
name, type = meth.to_s.match(/^(.+?)([=?]?)$/)[1..-1]
しかし、それは最善の解決策ではないようです= \
それをより良くする方法はありますか?ありがとう。
現時点で私はこのコードを持っています:
name, type = meth.to_s.match(/^(.+?)([=?]?)$/)[1..-1]
しかし、それは最善の解決策ではないようです= \
それをより良くする方法はありますか?ありがとう。
最良のオプションはこれのようです:
name, type = meth.to_s.split(/([?=])/)
これは大まかに私が私の実装する方法です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
または、1つの正規表現のみを評価するこのバージョン:
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