loading
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Example 1: Extending the Nil and String classes to check for nils and empty strings all at once
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
puts " 🔼Look ma. No empty strings or nils 😁😁😁"

# Example Two. A simple DSL for Roman Numerals
# I interpret missing methods as numerals to have a simple Ruby-like syntax 
class Roman
  def self.method_missing name, *args
    numeral = name.to_s
    numeral.gsub!("IV", "IIII")
    numeral.gsub!("IX", "VIIII")
    numeral.gsub!("XL", "XXXX")
    numeral.gsub!("XC", "LXXXX")
    numeral.gsub!("CD", "CCCC")
    numeral.gsub!("CM", "DCCCC")

    return  numeral.count("I") +
            numeral.count("V") * 5 +
            numeral.count("X") * 10 +
            numeral.count("L") * 50 +
            numeral.count("C") * 100 +
            numeral.count("D") * 500 +
            numeral.count("M") * 1000
  end
end

puts Roman.I # Foundation of Rome
puts Roman.DCCV # Julius Caesar crosses the Rubicon
puts Roman.DCCX # Julius Caesar assassinated
puts Roman.MCCXXIX # Western Roman Empire Falls

puts "Rome was sacked #{Roman.MCCXXIX - Roman.DCCX} years after the assassination of Caesar"
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]