Created
August 11, 2014 04:07
-
-
Save kmdsbng/bcbbf788efa6e2746ca7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- encoding: utf-8 -*- | |
| class BlankSlate < BasicObject | |
| def method_missing(name, *args) | |
| "BlankSlate##{name} called" | |
| end | |
| def respond_to?(name) | |
| name.to_s == 'hoge' | |
| end | |
| end | |
| class A | |
| def hoge | |
| 'moge' | |
| end | |
| end | |
| def main | |
| begin | |
| BasicObject.new.respond_to?(:==) # => | |
| rescue => x | |
| x # => #<NoMethodError: undefined method `respond_to?' for #<BasicObject:0x007ff4ab80f5a0>> | |
| end | |
| bs = BlankSlate.new | |
| bs.respond_to?(:hoge) # => true | |
| bs.respond_to?(:moge) # => false | |
| bs.respond_to?(:__send__) # => false | |
| bs.hoge # => "BlankSlate#hoge called" | |
| bs.__send__(:hoge) # => "BlankSlate#hoge called" | |
| Object.ancestors # => [Object, Kernel, BasicObject] | |
| BasicObject.instance_methods # => [:==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__] | |
| Kernel.instance_methods - BasicObject.instance_methods # => [:nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for] | |
| Object.instance_methods - Kernel.instance_methods # => [:==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__] | |
| Object.new.method(:instance_variable_set).methods - Object.instance_methods # => [:call, :[], :arity, :to_proc, :receiver, :name, :original_name, :owner, :unbind, :source_location, :parameters] | |
| Object.new.method(:instance_variable_set).source_location # => nil | |
| Object.new.method(:instance_variable_set).parameters # => [[:req], [:req]] | |
| Object.new.method(:instance_variable_set).arity # => 2 | |
| Object.new.method(:instance_variable_set).receiver # => #<Object:0x007ff4ab80c3c8> | |
| Object.new.method(:instance_variable_set).original_name # => :instance_variable_set | |
| Object.new.method(:instance_variable_set).name # => :instance_variable_set | |
| Object.new.method(:instance_variable_set).owner # => Kernel | |
| Object.new.method(:instance_variable_set).source_location # => nil | |
| Object.new.method(:instance_eval).source_location # => nil | |
| Object.new.method(:__id__).source_location # => nil | |
| A.new.method(:hoge).source_location # => ["/Users/kameda/work/basic_object_test.rb.rubyeval", 14] | |
| end | |
| case $PROGRAM_NAME | |
| when __FILE__ | |
| main | |
| when /spec[^\/]*$/ | |
| # {spec of the implementation} | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment