Cro*_*lio 8 ruby methods ruby-on-rails defined
class C1
unless method_defined? :hello # Certainly, it's not correct. I am asking to find something to do this work.
def_method(:hello) do
puts 'Hi Everyone'
end
end
end
Run Code Online (Sandbox Code Playgroud)
那么,如何判断方法是否定义?
Jör*_*tag 17
您发布的代码可以很好地检查方法是否已定义.Module#method_defined?是正确的选择.(还有变种Module#public_method_defined?,Module#protected_method_defined?和Module#private_method_defined?.)问题在于你的呼叫def_method,它不存在.(它被称为Module#define_method).
这就像一个魅力:
class C1
define_method(:hello) do
puts 'Hi Everyone'
end unless method_defined? :hello
end
Run Code Online (Sandbox Code Playgroud)
但是,由于您已经事先知道了名称并且没有使用任何闭包,因此无需使用Module#define_method,您只需使用def关键字:
class C1
def hello
puts 'Hi Everyone'
end unless method_defined? :hello
end
Run Code Online (Sandbox Code Playgroud)
或者我误解了你的问题你是否担心遗产?在这种情况下,Module#method_defined?它不是正确的选择,因为它遍历整个继承链.在这种情况下,您将不得不使用Module#instance_methods或使用其中一个表兄弟Module#public_instance_methods,Module#protected_instance_methods或者Module#private_instance_methods使用可选参数来告诉他们是否包含来自超类/ mixins的方法.(请注意,文件是错误的:如果你没有传递参数,这将包括所有继承的方法.)
class C1
unless instance_methods(false).include? :hello
def hello
puts 'Hi Everyone'
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是一个小测试套件,显示我的建议有效:
require 'test/unit'
class TestDefineMethodConditionally < Test::Unit::TestCase
def setup
@c1 = Class.new do
def self.add_hello(who)
define_method(:hello) do
who
end unless method_defined? :hello
end
end
@o = @c1.new
end
def test_that_the_method_doesnt_exist_when_it_hasnt_been_defined_yet
assert !@c1.method_defined?(:hello)
assert !@c1.instance_methods.include?(:hello)
assert !@o.methods.include?(:hello)
assert !@o.respond_to?(:hello)
assert_raise(NoMethodError) { @o.hello }
end
def test_that_the_method_does_exist_after_it_has_been_defined
@c1.add_hello 'one'
assert @c1.method_defined?(:hello)
assert @c1.instance_methods.include?(:hello)
assert @o.methods.include?(:hello)
assert_respond_to @o, :hello
assert_nothing_raised { @o.hello }
assert_equal 'one', @o.hello
end
def test_that_the_method_cannot_be_redefined
@c1.add_hello 'one'
assert @c1.method_defined?(:hello)
assert @c1.instance_methods.include?(:hello)
assert @o.methods.include?(:hello)
assert_respond_to @o, :hello
assert_nothing_raised { @o.hello }
assert_equal 'one', @o.hello
@c1.add_hello 'two'
assert @c1.method_defined?(:hello)
assert @c1.instance_methods.include?(:hello)
assert @o.methods.include?(:hello)
assert_respond_to @o, :hello
assert_nothing_raised { @o.hello }
assert_equal 'one', @o.hello, 'it should *still* respond with "one"!'
end
end
Run Code Online (Sandbox Code Playgroud)