Permalink
Browse files

polish examples

  • Loading branch information...
1 parent 7b5c716 commit b107220e955bf57e0ec83a9b743de8bcb365c585 @JoshCheek committed Jun 30, 2011
Showing with 10 additions and 9 deletions.
  1. +1 −6 session1/examples/2_check_odd.rb
  2. +2 −1 session1/examples/3_rememberer.rb
  3. +7 −2 session1/examples/4_rememberer2.rb
@@ -1,11 +1,6 @@
# invoke this program with a number
# $ ruby 2_check_odd.rb number
-def is_odd?(number)
- # it is odd if it has a remainder of 1 when divided by two
- number % 2 == 1
-end
-
# when a user passes an argument to a ruby program
# it is stored in the ARGV array
@@ -19,7 +14,7 @@ def is_odd?(number)
# We'll have to turn it into a number ourselves with to_i (what if the user didn't pass a number?)
number = ARGV.first.to_i
-if is_odd?(number)
+if number.odd?
puts "Yes, #{number} is odd"
else
puts "No, #{number} is not odd"
@@ -16,4 +16,5 @@
puts "No, I haven't seen #{line} before"
lines_seen << line
end
-end
+
+end
@@ -1,5 +1,10 @@
# Did you understand the last one?
-# This is the same one, but done a little differently
+# This is the same one, but we abstracted the tasks
+# into methods that are easier to test,
+# and can be reused easily. They also let us talk
+# about a task based on the method name, which is
+# much simpler than the piece of code behind it.
+
def get_line
gets.chomp
@@ -28,7 +33,7 @@ def record_observation(lines_seen, line)
loop do
line = get_line
- break if finished? line
+ break if finished?(line)
puts user_message(lines_seen, line)
record_observation(lines_seen, line)
end

0 comments on commit b107220

Please sign in to comment.