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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#RUBYISMS

a = b.foo
if a.empty?
  a = b.bar
else
  a.reverse!
end

#can be simplied a little by assigning and 
#checking returned boolean type

if (a = b.foo).empty?
  a = b.bar
else
  a.reverse!
end

-----------------------------------------------

#check the most obvious case first in conditions
case savings_account
when "not much"
  buy_bike
when "some dough"
  buy_yugo
when "a good wad in the wallet"
  buy_toyota
when "just won the lottery"
  buy_dream_car
end

-----------------------------------------------

#not good
breakfast = nil
if breakfeast == nil
  breakfast = "bacon and egss"
end

#not bad
if breakfast.nil?
  breakfast = "bacon and eggs"
end

#pretty cool
if !breakfast
  breakfast = "bacon and eggs"
end

# if !something is the same as unless something
unless breakfeast
  breakfast = "bacon and eggs"
end

#can it get better?  awww yea!
breakfast = "bacon and eggs" unless breakfast

#even better???!!!
breakfast ||= "bacon and eggs"

-----------------------------------------------

#too much work, ugh!
val = 12
if val == 1 || val == 12 || val == 42
  puts "woo hoo!"
end

#sweetness
if [1,12,42].include?(val)
  puts "woo hoo!"
end

-----------------------------------------------

#only include value if it is not present in array

fruits = ['apple','banana']

#ouch
fruits << 'apple' unless fruits.include?('apple')

#better
fruits |= ['apple']

-----------------------------------------------

#a lot of work
user_id = nil
if comments
  if comments.first
    if comments.first.user
      user_id = comments.first.user.id
    end
  end
end

#are they really paying me to do this
#evaluate logic from left to right, returning last value executed
user_id = comments && comments.first && comments.first.user && comments.first.user.id

-----------------------------------------------

#return values from methods
def say_goodnight(name)
  result = "Good night, " + name.capitalize
  return result
end

#rubyized
#remember last value of method gets returned by default
#use interpolated values instead of concatenating
def say_goodnight(name)
  "Good night, #{name,capitalize}"
end

-----------------------------------------------

def old_enough?(age)
  if age > 15
    true
  else
    false
  end
end

#would be better written as
def old_enough?(age)
  age > 15
end

-----------------------------------------------

#Classes getters/setters

class Person
  def initialize(name)
    @name = name
  end
  def name
    @name
  end
  def name=(name)
    @name = name
  end
end

#use attr_accessor to do the same thing, sweetness!
class Person
  attr_accessor :name
  def initialize(name)
    @name = name
  end
end

-----------------------------------------------

a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ]

#sometimes its easier to create this array alternatively like this
a = %w{ ant bee cat dog elk }

-----------------------------------------------

for i in 0..9
  puts i, " "
end

#this is so much cooler!
10.times {|i| puts i}

#don't forget other iterators like upto, downto, and step

-----------------------------------------------

people = array of people...

#PAINFULL!!
i = 0
while i < people.size
  puts people[i].name
  i += 1
end

#LESS PAINFUL!
for i in 0..people.size
  puts people[i].name
end

#rubyize it by using the each iterator
people.each { |person| puts person.name }

-----------------------------------------------
#call makes 26 calls for each letter everytime 
#capital_letters method is called
def capital_letters
  ('A'..'Z').to_a
end

#instead, creating a class variable caches the to_a in memory
#the method call uses cached value
@@capital_letters=('A'..'Z').to_a
def capital_letters
  @@capital_letters
end

-----------------------------------------------

#don't reinvent the wheel, check the built in api
def stringify_array(an_array)
  a_string = ""
  for element in an_array
    if a_string.size > 0
      a_string += ", "
    end
    a_string += element.to_s
  end
  return a_string
end

some_array = %w(hello there buddy)
puts stringify_array(some_array)

#join gives us exactly what we need
puts some_array.join(", ")