Class: Greeter

Inherits:
ApplicationController show all
Defined in:
lib/spec/virtualizees/greeter.rb

Instance Method Summary collapse

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_tenObject



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_allObject

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_blockObject



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_elseObject

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_elseObject

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_elseObject

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_elseObject

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_nestedObject



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_ifObject

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_unlessObject

Postfix unless



64
65
66
# File 'lib/spec/virtualizees/greeter.rb', line 64

def greet_postfix_unless
  'Goodbye (postfix unless)' unless @hello
end

#greet_unlessObject

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_elseObject

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