411 REFERENCE · Wikibooks
Ruby Programming/Reference/Predefined Classes
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
In ruby even the base types (also predefined classes) can be hacked.quirks In the following example, 5 is an immediate,immediate a literal, an object, and an instance of Fixnum.
class Fixnum
alias other_s to_s
def to_s()
a = self + 5
return a.other_s
end
end
a = 5
puts a.class ## prints Fixnum
puts a ## prints 10 (adds 5 once)
puts 0 ## prints 5 (adds 5 once)
puts 5 ## prints 10 (adds 5 once)
puts 10 + 0 ## prints 15 (adds 5 once)
b = 5+5
puts b ## puts 15 (adds 5 once)
Footnotes
immediate Which means the 4 VALUE bytes are not a reference but the value itself. All 5 have the same object id (which could also be achieved in other ways).
quirks Might not always work as you would like, the base types don't have a constructor (def initialize), and can't have singleton methods. There are some other minor exceptions.