Skip to content

Instantly share code, notes, and snippets.

@phlipper
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phlipper/3e7f919b768f02b4a3b4 to your computer and use it in GitHub Desktop.
Save phlipper/3e7f919b768f02b4a3b4 to your computer and use it in GitHub Desktop.
TECH603 Day 3 Warmup
# 1. Create a `day_3` directory under the course directory.
# 2. Save this file under the new folder as `warmup.rb`.
# 3. Assign any string value to a variable.
string = "i hope i do well in class today"
# 4. Assign any whole number to a variable.
b = 42
# 5. Assign any decimal number to a variable.
c = 1.6
# 6. Add each of the previous variables to a collection.
collection = [string, b, c]
# 7. Output the number of items in the collection.
puts collection.count
# 8. Iterate through the collection and output each item.
collection.each do |fuzzy|
puts fuzzy
end
for fuzzy in collection
puts fuzzy
end
# 9. Create a dictionary with your name, birth month, and birth day.
dictionary = {
"name" => "Phil",
"birth_month" => "January",
"birth_day" => 1
}
# 10. Using the dictionary from the previous step, output the following message:
# My name is _ and my birthday is on _.
puts "My name is #{dictionary["name"]} and my birthday is on #{dictionary["birth_month"]} #{dictionary["birth_day"]}."
# 11. Add the dictionary from step 10 to the collection of items from step 6.
collection.push(dictionary)
puts collection.to_s
# 12. Create a function which returns an "ordinal indicator" for a number.
#
# In written languages, an ordinal indicator is a character, or group of
# characters, following a numeral denoting that it is an ordinal number,
# rather than a cardinal number.
# In English orthography, this corresponds to the suffixes -st, -nd, -rd,
# -th in written ordinals represented as 1st, 2nd, 3rd, 4th, etc.
def ordinal_indicator(number)
return number if number.to_s != number.to_i.to_s
last_two = number.to_s[-2..-1]
return "#{number}th" if ["11", "12", "13"].include?(last_two)
case number.to_s[-1]
when "1" then "#{number}st"
when "2" then "#{number}nd"
when "3" then "#{number}rd"
else "#{number}th"
end
end
[nil, "one", 23.5, 1, 2, 3, 4, 5, 11, 21, 211, 212, 221, 223, 222, 1000].each do |number|
puts ordinal_indicator(number).inspect
end
# early version of our function
def ordinal_indicator(number)
# "1" => "1st"
# "2" => "2nd"
if number.to_i == 1
return "1st"
elsif number.to_i == 2
return "2nd"
end
end
# in-progress version of our function
def ordinal_indicator(number)
# "1" => "1st"
# "2" => "2nd"
# int = number.to_i
last_number = number.to_s[-1]
last_two = number.to_s[-2..-1]
if ["11", "12", "13"].include?(last_two)
return "#{number}th"
elsif last_number == "1"
return "#{number}st"
elsif last_number == "2"
return "#{number}nd"
elsif last_number == "3"
return "#{number}rd"
else
return "#{number}th"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment