Class: Greeter
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Greeter
- Defined in:
- lib/spec/virtualizees/greeter.rb
Instance Method Summary collapse
- #count_to_ten ⇒ Object
-
#greet_all ⇒ Object
All together now.
- #greet_block ⇒ Object
-
#greet_if_else ⇒ Object
An example conditional: if and else.
-
#greet_if_then_else ⇒ Object
If, then, else.
-
#greet_if_then_no_else ⇒ Object
If, then, no else.
-
#greet_if_without_else ⇒ Object
If without else.
- #greet_nested ⇒ Object
-
#greet_postfix_if ⇒ Object
Postfix if.
-
#greet_postfix_unless ⇒ Object
Postfix unless.
-
#greet_unless ⇒ Object
Unless.
-
#greet_unless_else ⇒ Object
Unless, then else.
-
#initialize(hello) ⇒ Greeter
constructor
A new instance of Greeter.
Constructor Details
#initialize(hello) ⇒ Greeter
Returns a new instance of Greeter.
2 3 4 |
# File 'lib/spec/virtualizees/greeter.rb', line 2 def initialize(hello) @hello = hello end |
Instance Method Details
#count_to_ten ⇒ Object
122 123 124 125 126 |
# File 'lib/spec/virtualizees/greeter.rb', line 122 def count_to_ten [1..10].each do |index| puts index end end |
#greet_all ⇒ Object
All together now
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/spec/virtualizees/greeter.rb', line 69 def greet_all result = '' # 1 if @hello result = 'Hello' else result = 'Goodbye' end # 2 if 2 + 2 == 4 result += '\nMath is right' end # 3 result += '\nThis is supposed to look like English' if false # 4 unless 2 + 9 == 10 result += '\nMath should work in unless too' end # 5 result += '\nWorld!' unless true result end |
#greet_block ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/spec/virtualizees/greeter.rb', line 108 def greet_block # Multiple statements in the if/else clauses if @hello value = 5 value += 5 value else thing = 9 thing end end |
#greet_if_else ⇒ Object
An example conditional: if and else
15 16 17 18 19 20 21 22 23 |
# File 'lib/spec/virtualizees/greeter.rb', line 15 def greet_if_else if @hello 'Hello World! (if else)' else # Compound expressions should be preserved # (i.e. not evaluated too early or in the wrong context) 'Good' + 'bye (if else)' end end |
#greet_if_then_else ⇒ Object
If, then, else
38 39 40 |
# File 'lib/spec/virtualizees/greeter.rb', line 38 def greet_if_then_else if @hello then 'Hello World! (if then else)' else 'Goodbye (if then else)' end end |
#greet_if_then_no_else ⇒ Object
If, then, no else
43 44 45 |
# File 'lib/spec/virtualizees/greeter.rb', line 43 def greet_if_then_no_else if @hello then 'Hello World! (if then)' end end |
#greet_if_without_else ⇒ Object
If without else
26 27 28 29 30 |
# File 'lib/spec/virtualizees/greeter.rb', line 26 def greet_if_without_else if @hello 'Hello World! (if without else)' end end |
#greet_nested ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/spec/virtualizees/greeter.rb', line 93 def greet_nested if true # This if should be processed, even though it never happens! if 2 + 2 == 4 'Math is right' else 'Weird' end else # This if should be expanded, but NOT evaluated! puts 'hi there' if true 'The false case' end end |
#greet_postfix_if ⇒ Object
Postfix if
33 34 35 |
# File 'lib/spec/virtualizees/greeter.rb', line 33 def greet_postfix_if 'Hello World! (postfix if)' if @hello end |
#greet_postfix_unless ⇒ Object
Postfix unless
64 65 66 |
# File 'lib/spec/virtualizees/greeter.rb', line 64 def greet_postfix_unless 'Goodbye (postfix unless)' unless @hello end |
#greet_unless ⇒ Object
Unless
48 49 50 51 52 |
# File 'lib/spec/virtualizees/greeter.rb', line 48 def greet_unless unless @hello 'Goodbye (unless)' end end |
#greet_unless_else ⇒ Object
Unless, then else
55 56 57 58 59 60 61 |
# File 'lib/spec/virtualizees/greeter.rb', line 55 def greet_unless_else unless @hello 'Goodbye (unless else)' else 'Hello World! (unless else)' end end |