Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ More Ruby programming. Create a git branch for ruby-collections-lesson. Then a

(1) Write a program which asks for a person's first name, then middle, then last. It should store each of these parts in an array. Finally, it should greet the person using their full name. Call the program full_name.rb.


(2) Write a program called sorted_words.rb. It should prompt the user for words and add each to an array. The user should be able to add as many words as they like, until they just hit enter to return a blank word. Then sort the array using the sort method and print it out.

(3) Write a program with a function add_up(i) . It is to be passed a positive integer, and it will add all the numbers from 1 to that integer and return the sum. Call the function three times within the program, and each time print out the return value. Call the program add_up.rb.
Expand Down
6 changes: 6 additions & 0 deletions add_up.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def add_up(a)
sum = 0
sum = (1..a).inject{ |s, i| s + i }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of inject here!

return sum
end
puts add_up(10)
11 changes: 11 additions & 0 deletions full_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
puts "Enter your name"
name = gets.chomp
name = name.to_s
puts "Enter your middle name"
middle_name = gets.chomp
middle_name = middle_name.to_s
puts "Enter your last name"
last_name = gets.chomp
last_name = last_name.to_s
full_name = ['name', 'middle_name', 'last_name']
puts "Hello " + name + " " + middle_name + " " + last_name + "!"
14 changes: 14 additions & 0 deletions leap_year.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
puts "Enter starting_year"
starting_year = gets.to_i
puts "Enter ending_year"
ending_year = gets.to_i
puts ""
while starting_year.to_i <= ending_year.to_i
if starting_year % 4 == 0
puts starting_year
elsif starting_year % 100 == 0
elsif starting_year % 400 == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the conditional (if/else) statement, we like to start with edge cases and then other possibilities.
So here the if/else block would be:

if starting_year % 400 == 0
    puts "#{starting_year} is leap year."
elsif starting_year % 4 = 0 && starting_yea % 100 != 0 
    puts "#{starting_year} is leap year."
end

puts starting_year
end
starting_year = starting_year.to_i + 1
end
8 changes: 8 additions & 0 deletions sorted_words.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
puts "Enter as many words as you want, hit enter key twice to exit the game"
word = gets.to_s
words = []
until (word = gets.chomp).empty?
words << word
puts "Sorted array: #{words.join(" , ")}/"
end
puts "Sorted words: #{words.sort.join(" , ")}."