|
| 1 | +class Person { |
| 2 | + def initialize: @name age: @age { |
| 3 | + """ |
| 4 | + This is a docstring for the Person constructor method. |
| 5 | + Docstrings usually are multi-line, like this one. |
| 6 | + """ |
| 7 | + } |
| 8 | + |
| 9 | + def to_s { |
| 10 | + # return is optional in this case, but we use it nontheless |
| 11 | + return "Person with name: #{@name inspect} and age: #{@age}" |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class PersonWithCity : Person { |
| 16 | + def initialize: @name age: @age city: @city { |
| 17 | + } |
| 18 | + |
| 19 | + def to_s { |
| 20 | + super to_s ++ " living in: #{@city inspect}" |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +p1 = Person new: "Johnny Jackson" age: 42 |
| 25 | +p1 println # prints: Person with name: "Johnny Jackson" and age: 42 |
| 26 | + |
| 27 | +p2 = PersonWithCity new: "John Appleseed" age: 55 city: "New York" |
| 28 | +p2 println # prints: Person with name: "John Appleseed" age: 55 living in: "New York" |
| 29 | + |
| 30 | +array = [1,2,3, "foo", 'bar] |
| 31 | +hash = <['foo => "bar", 'bar => 42]> |
| 32 | +tuple = (1,2,"hello","world") |
| 33 | +block = |x, y| { |
| 34 | + x + y println |
| 35 | +} |
| 36 | +block call: [4,2] |
| 37 | + |
| 38 | +0b010101 & 0b00101 to_s: 2 . println |
| 39 | +0xFF & 0xAB to_s: 16 . println |
| 40 | +0o77 > 0o76 println |
| 41 | +123.123 + 0.222 println |
| 42 | + |
| 43 | +x = 0 |
| 44 | +try { |
| 45 | + 10 / x println |
| 46 | +} catch ZeroDivisionError => e { |
| 47 | + x = 3 |
| 48 | + retry |
| 49 | +} finally { |
| 50 | + "Finally, done!" println |
| 51 | +} |
| 52 | + |
| 53 | +def a_method: arg1 with_default_arg: arg2 (42) { |
| 54 | + arg1 * arg2 println |
| 55 | +} |
| 56 | + |
| 57 | +a_method: 42 |
| 58 | +a_method: 42 with_default_arg: 85 |
| 59 | + |
| 60 | +class ClassWithClassMethod { |
| 61 | + def self class_method1 { |
| 62 | + 'works |
| 63 | + } |
| 64 | + |
| 65 | + def ClassWithClassMethod class_method2 { |
| 66 | + 'this_as_well |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +ClassWithClassMethod class_method1 println |
| 71 | +ClassWithClassMethod class_method2 println |
| 72 | + |
| 73 | +def another_method: block { |
| 74 | + 1 upto: 10 . map: block |
| 75 | +} |
| 76 | + |
| 77 | +# local returns |
| 78 | +another_method: |x| { return_local x * 2 } . inspect println |
| 79 | + |
| 80 | + |
| 81 | +# pattern matching: |
| 82 | +class PatternMatching { |
| 83 | + def match_it: obj { |
| 84 | + match obj { |
| 85 | + case String -> "It's a String!" println |
| 86 | + case Fixnum -> "It's a Number!" println |
| 87 | + case _ -> "Aything else!" println |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + def match_with_extract: str { |
| 92 | + match str { |
| 93 | + # m holds the MatchData object, m1 & m2 the first and second matches |
| 94 | + case /^(.*) : (.*)$/ -> |m, m1, m2| |
| 95 | + "First match: #{m1}" println |
| 96 | + "Second match: #{m2}" println |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +pm = PatternMatching new |
| 102 | +pm match_it: "foo" |
| 103 | +pm match_it: 42 |
| 104 | +pm match_it: 'foo |
| 105 | + |
| 106 | +pm match_with_extract: "Hello : World!" |
| 107 | + |
| 108 | + |
| 109 | +# calling ruby methods: |
| 110 | +[3, 2, 1] reverse() each() |a| { puts(a) } |
| 111 | +"Hello" sub("ll", "y") println |
| 112 | +[3, 2, 1] map() |a| { a * 2 } inject(0) |s i| { s + i } println |
0 commit comments