Skip to content

Commit

Permalink
adding 33
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelakey committed Dec 28, 2011
1 parent 624bf38 commit 5fdb63a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ex33.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env ruby

i = 0
numbers = []

while i < 6
puts "At the top i is #{i}"
numbers.push(i)

i += 1
puts "Array numbers contains: #{numbers}"
puts "At the bottom i is #{i}"
end

puts "The numbers: "

for num in numbers
puts num
end
23 changes: 23 additions & 0 deletions ex33_0.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env ruby

i = 0
numbers = []

while i < 6
puts "At the top i is #{i}"
numbers.push(i)

i += 1
puts "Array numbers contains: #{numbers}"
puts "At the bottom i is #{i}"
end

puts "The numbers: "

#for num in numbers
# puts num
#end

numbers.each do |num|
puts num
end
45 changes: 45 additions & 0 deletions ex33_1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env ruby

# Get a number from the command line input
# Convert it to an integer .to_i before we save it to the variable number_got
puts "Gimme a number: "
number_got = gets.to_i

# Define a method with a really long name
# This method takes one parameter, or argument
#
def take_number_iterate_array_print(number_input)
# I have to set these up in here otherwise the method can't access them
# Assign the starting number i.e. "number_current" to 0
# Assign an empty array to "numbers"
number_current = 0
numbers = []
while number_current < number_input
puts "Number top: #{number_current}"
numbers.push(number_current)

# Take whatever number_current currently is and add 1 to it and save that back to number_current
# If number_current is 0 we add 1 making it 1 and save that to number_current instead
number_current += 1
puts "Array: #{numbers}"
puts "Number bottom: #{number_current}"
end

puts "The numbers: "

# Iterate over each item in the array "numbers"
# Pass each item to the "num" paramemter, or argument, in the anonymous function
# puts "num", the currently iterated item from the array
numbers.each do |num|
puts num
end
end

# Call the method
# Pass the number we got from the command line into the method as the parameter, or argument "number_got"
take_number_iterate_array_print(number_got)


# Extra Credit:
# 1. Convert this while loop to a function that you can call, and replace 6 in the test (i < 6) with a variable.
# 2. Now use this function to rewrite the script to try different numbers.
48 changes: 48 additions & 0 deletions ex33_3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env ruby

# Get a number from the command line input
# Convert it to an integer .to_i before we save it to the variable number_got
puts "Gimme a number: "
number_got = gets.to_i

puts "Gimme a number to iterate by: "
iterator_got = gets.to_i

# Define a method with a really long name
# This method takes 2 parameters, or arguments
#
def take_number_iterate_array_print(number_input, increment_by)
# I have to set these up in here otherwise the method can't access them
# Assign the starting number i.e. "number_current" to 0
# Assign an empty array to "numbers"
number_current = 0
numbers = []
while number_current < number_input
puts "Number top: #{number_current}"
numbers.push(number_current)

# Take whatever number_current currently is and add the increment_by variable to it and save that back to number_current
# If number_current is 0 and the increment_by value is 1 then we add 1 making it 1 and save that to number_current instead
number_current += increment_by
puts "Array: #{numbers}"
puts "Number bottom: #{number_current}"
end

puts "The numbers: "

# Iterate over each item in the array "numbers"
# Pass each item to the "num" paramemter, or argument, in the anonymous function
# puts "num", the currently iterated item from the array
numbers.each do |num|
puts num
end
end

# Call the method
# Pass the number we got from the command line into the method as the parameter, or argument "number_got"
take_number_iterate_array_print(number_got, iterator_got)


# Extra Credit:
# 3. Add another variable to the function arguments that you can pass in that lets you change the + 1 on line 8 so you can change how much it increments by.

0 comments on commit 5fdb63a

Please sign in to comment.