You can change the definition of any class at any time
class NilClass
def blank?
true
end
end
class String
def blank?
self.size == 0
end
end
["", "person", nil].each do |element|
puts element unless element.blank?
end
def self.method_missing(name, *args)
...
end
class Roman
def self.method_missing name, *args
roman = name.to_s
roman.gsub!("IV", "IIII")
roman.gsub!("IX", "VIIII")
roman.gsub!("XL", "XXXX")
roman.gsub!("XC", "LXXXX")
(roman.count("I") +
roman.count("V") * 5 +
roman.count("X") * 10 +
roman.count("L") * 50 +
roman.count("C") * 100)
end
end
puts Roman.X
puts Roman.XC
puts Roman.XII
puts Roman.X
User for "Acts like a..." relationships
as opposed to "Has a..."
class ActsAsCsv
...
def self.acts_as_csv
...
end
...
end
class RubyCsv < ActsAsCsv
acts_as_csv
end
Use for "Acts like a..." relationships
as opposed to "Has a..."
class ActsAsCsv
...
def self.acts_as_csv
...
end
...
end
class RubyCsv
include ActsAsCsv
acts_as_csv
end