class HasMethod
def has_method
'has_method'
end
end
class NotHaveMethod
end
has_method = HasMethod.new
not_have_method = NotHaveMethod.new
p has_method.respond_to? :has_method
not_have_method.respond_to? :has_method
true
false
class HasPrivateMethod
private
def private_method
'private_method'
end
end
has_private_method = HasPrivateMethod.new
p has_private_method.respond_to? :private_method
p has_private_method.respond_to? :private_method, true # give true for second argment
has_private_method.respond_to? :private_method, false # give false for second argment
false true
false
class RespondAtMethodMissing
def method_missing(method_name, *args)
if method_name == :respond_at_method_missing
'respond_at_method_missing'
else
super
end
end
end
respond_at_method_missing = RespondAtMethodMissing.new
p respond_at_method_missing.respond_at_method_missing
p respond_at_method_missing.respond_to? :respond_at_method_missing
class RespondAtMethodMissing
def respond_to_missing?(method_name, include_private = false)
if method_name.to_sym == :respond_at_method_missing
true
else
super
end
end
end
respond_at_method_missing.respond_to? :respond_at_method_missing
"respond_at_method_missing" false
true
class Hello
def hi
'hi'
end
def hello(name)
"hello #{name}"
end
end
hello = Hello.new
p hello.send :hi
hello.send :hello, "takkanm"
"hi"
"hello takkanm"
hello.send :hi, "foo"
ArgumentError: wrong number of arguments (given 1, expected 0) (pry):108:in `hi' (pry):120:in `<main>' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:387:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:387:in `evaluate_ruby' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:355:in `handle_line' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:274:in `block (2 levels) in eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:273:in `catch' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:273:in `block in eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:272:in `catch' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:272:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/backend.rb:66:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/backend.rb:12:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/kernel.rb:87:in `execute_request' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/kernel.rb:47:in `dispatch' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/kernel.rb:37:in `run' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/command.rb:70:in `run_kernel' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/command.rb:34:in `run' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/bin/iruby:5:in `<top (required)>' C:/Ruby24-x64/bin/iruby:23:in `load' C:/Ruby24-x64/bin/iruby:23:in `<main>'
class Hello
def bye(*args)
"bye #{args}"
end
end
hello.send :bye, "btw","who are you?"
"bye [\"btw\", \"who are you?\"]"
class Hello
protected
def protected_hello
'protected hello'
end
private
def private_hello
'private hello'
end
end
hello = Hello.new
hello.protected_hello
NoMethodError: protected method `protected_hello' called for #<Hello:0x00000006b44190> Did you mean? protected_methods (pry):168:in `<main>' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:387:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:387:in `evaluate_ruby' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:355:in `handle_line' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:274:in `block (2 levels) in eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:273:in `catch' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:273:in `block in eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:272:in `catch' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:272:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/backend.rb:66:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/backend.rb:12:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/kernel.rb:87:in `execute_request' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/kernel.rb:47:in `dispatch' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/kernel.rb:37:in `run' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/command.rb:70:in `run_kernel' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/command.rb:34:in `run' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/bin/iruby:5:in `<top (required)>' C:/Ruby24-x64/bin/iruby:23:in `load' C:/Ruby24-x64/bin/iruby:23:in `<main>'
p hello.send :protected_hello
hello.private_hello
"protected hello"
NoMethodError: private method `private_hello' called for #<Hello:0x00000006b44190> Did you mean? private_methods (pry):171:in `<main>' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:387:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:387:in `evaluate_ruby' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:355:in `handle_line' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:274:in `block (2 levels) in eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:273:in `catch' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:273:in `block in eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:272:in `catch' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/pry-0.12.2/lib/pry/pry_instance.rb:272:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/backend.rb:66:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/backend.rb:12:in `eval' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/kernel.rb:87:in `execute_request' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/kernel.rb:47:in `dispatch' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/kernel.rb:37:in `run' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/command.rb:70:in `run_kernel' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/lib/iruby/command.rb:34:in `run' C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/iruby-0.3/bin/iruby:5:in `<top (required)>' C:/Ruby24-x64/bin/iruby:23:in `load' C:/Ruby24-x64/bin/iruby:23:in `<main>'
hello.send :private_hello
"private hello"
class MailSender
attr_reader :from
def initialize(from)
@from = from
end
def send(to)
"send mail: #{to}"
end
end
mail_sender = MailSender.new("takkanm")
p mail_sender.send 'takkanm@example.com'
mail_sender.__send__ :from
"send mail: takkanm@example.com"
"takkanm"
次回 "10-2 クラスについて調べる" から