More experimenting with type stuff
Download this source
b = 57.5
c = 82.1
# Get all living objects of a type
puts "All Numeric objects in the system:"
ObjectSpace.each_object(Numeric) { | it |
puts it
}
# 'Reflect' objects
puts "\nMethods on range:"
range = 5..8
methods = range.methods
puts methods[0..4]
# Check specific methods / operators
puts "\nHas 'min' method? #{range.methods.member?('min')}"
puts "Has 'minulation' method? #{range.respond_to?('minulation')}"
puts "Has equals overload? #{range.respond_to?('==')}"
# Get instance id, class and check inheritance
# Notice the difference between is_a? and kind_of?
num = 88
puts "\nId is #{num.object_id}"
puts "Class is #{num.class}"
puts "Is a kind of Fixnum? #{num.kind_of?(Fixnum)}"
puts "Is a kind of Numeric? #{num.kind_of? Numeric}"
puts "Is an instance of Fixnum? #{num.instance_of?(Fixnum)}"
puts "Is an instance of Numeric? #{num.instance_of? Numeric}"
Running this outputs:
All Numeric objects in the system:
82.1
57.5
2.71828182845905
3.14159265358979
2.22044604925031e-16
1.79769313486232e+308
2.2250738585072e-308
Methods on range:
find_all
each
object_id
inject
singleton_methods
Has 'min' method? true
Has 'minulation' method? false
Has equals overload? true
Id is 177
Class is Fixnum
Is a kind of Fixnum? true
Is a kind of Numeric? true
Is an instance of Fixnum? true
Is an instance of Numeric? false

