Skip to content

Instantly share code, notes, and snippets.

@phlipper
Created September 27, 2014 22:57
Show Gist options
  • Save phlipper/482b4779f3ad4b2abc52 to your computer and use it in GitHub Desktop.
Save phlipper/482b4779f3ad4b2abc52 to your computer and use it in GitHub Desktop.
TECH 601-00 - Examples and Solutions
colors = ["purple", "yellow", "blue", "white", "red", "green", "orange"]
puts colors[4]
puts colors[40]
puts colors.index("green")
puts colors.index("foo")
colors2 = [
["tuesday", "purple"],
["saturday", "yellow"],
["wednesday", "blue"],
["sunday", "white"],
["monday", "red"],
["thursday", "green"],
["friday", "orange"]
]
# puts colors2[0].inspect
# puts colors2[0][1].inspect
colors2.each_with_index do |pair, index|
puts "#{pair} at index #{index}"
pair.each_with_index do |day, color|
puts "#{day} is #{color}"
end
end
colors3 = {
"tuesday" => "purple",
"saturday" => "yellow",
"wednesday" => "blue",
"sunday" => "white",
"monday" => "red",
"thursday" => "green",
"friday" => "orange"
}
puts colors3["thursday"]
# original data
# https://www.cisweb1.unr.edu/cxs/CourseListing.asp?master_id=1955&course_area=BASE&course_number=105&course_subtitle=00
data = File.read("data.txt")
lines = data.split("\n")
# puts line.inspect
course = {}
lines.each do |line|
heading, content = line.split(":")
puts "heading: #{heading}"
puts "content: #{content}"
course[heading] = content.to_s.strip
end
puts course.inspect
Course: BASE 105-00
Term: 2145
Section Number: 2
Schedule Number: 2145BASE1052
Instructor(s): Jay Johnson, Mark Kertenian
Location: William Peccole Park - UNR
Dates: July 7 - 11, 2014 (Schedule TBA)
Units: Non-Credit
Notes: Additional Information
Upon registration, you will receive a confirmation via email, so please provide your email address during registration.
NEW THIS YEAR: All camp health forms, behavior guidelines, maps and other paperwork will be posted online for download at www.unr.edu/sportscamps. Please complete the health form and behavior form and return them as soon as possible.
Full payment is due upon registration. A $50 cancellation fee will be charged for participant-initiated cancellations made prior to camp. No refunds will be given after the start of camp.
Fee includes instruction, and camp t-shirt and hat.
# 1. create a variable named 'a' with the value of 1 and display 'a'
a = 1
puts a
# 2. create a variable named 'b' with the value of 2 and display 'b'
b = 2
puts b
# 3. create a variable named 'c' with the value of 3 and display 'c'
# 4. create a variable named 'd' with the value of 10 and display 'd'
# 5. add 'a' and 'b' and display the result
# 6. add 'a' and 'd' and display the result
# 7. add 'a' and 'b' and 'c' and 'd' and display the result
# 8. multiply 'd' and 'b' and display the result
# 9. multiple 'd' by itself and display the result
# 10. divide 'd' by 'b' and display the result
# 11. divide 'd' by itself and display the result
# 12. create a list named 'lots_of_a' that contains 4 'a's
# 13. display contents of the list 'lots_of_a'
# 14. create a list named numbers that contains 'a', 'b', 'c', and 'd' and display the list
# 15. create a variable named 'name' that contains your full name
# 16. create a variable named 'greeting' that contains the words "Hello, my name is: "
# 17. create a varialbe named 'full_greeting' that is made up of 'greeting' and 'name' and display the result
# 18. iterate through 'numbers' and display "The value is: " with each value
# 19. get the sum of 'numbers' and display the result
# 20. get the average of 'numbers' and display the result
# BONUS - What happens if you try to run the following (and why)?:
# "1" + 1
# What could we do to make that run?
# 1. create a variable named 'a' with the value of 1 and display 'a'
a = 1
puts a
# 2. create a variable named 'b' with the value of 2 and display 'b'
b = 2
puts b
# 3. create a variable named 'c' with the value of 3 and display 'c'
c = 3
puts c
# 4. create a variable named 'd' with the value of 10 and display 'd'
d = 10
puts d
# 5. add 'a' and 'b' and display the result
result = a + b
puts result
# 6. add 'a' and 'd' and display the result
puts a + d
# 7. add 'a' and 'b' and 'c' and 'd' and display the result
puts a + b + c + d
# 8. multiply 'd' and 'b' and display the result
puts d * b
# 9. multiple 'd' by itself and display the result
puts d * d
# 10. divide 'd' by 'b' and display the result
puts d / b
# 11. divide 'd' by itself and display the result
puts d / d
# 12. create a list named 'lots_of_a' that contains 4 'a's
lots_of_a = [a, a, a, a]
# 13. display contents of the list 'lots_of_a'
puts lots_of_a
# 14. create a list named numbers that contains 'a', 'b', 'c', and 'd' and display the list
numbers = [a, b, c, d]
puts numbers
# 15. create a variable named 'name' that contains your full name
name = "Phil"
# 16. create a variable named 'greeting' that contains the words "Hello, my name is: "
greeting = "Hello, my name is: "
# 17. create a varialbe named 'full_greeting' that is made up of 'greeting' and 'name' and display the result
full_greeting = greeting + name
puts full_greeting
# 18. iterate through 'numbers' and display "The value is: " with each value
for number in numbers
"The value is: #{number}"
end
numbers.each do |number|
"The value is: #{number}"
end
numbers.each { |number| "The value is: #{number}" }
# 19. get the sum of 'numbers' and display the result
sum = 0
numbers.each do |number|
sum = sum + number
end
puts "Sum: #{sum}"
sum = 0
numbers.each do |number|
sum += number
end
puts "Sum: #{sum}"
sum = numbers.reduce(0) { |counter, number| counter += number }
puts "Sum: #{sum}"
# 20. get the average of 'numbers' and display the result
sum = numbers.reduce(0) { |sum, num| sum += num }
count = numbers.size
puts "Average: #{sum / count}"
# BONUS - What happens if you try to run the following (and why)?:
# "1" + 1
# What could we do to make that run?
s1 = "1"
n1 = 1
puts ".to_s - #{s1 + n1.to_s}"
puts ".to_i - #{s1.to_i + n1}"
# 1. assign the string "5" to a variable and display the value
# 2. assign the integer 5 to a variable and display the value
# 3. add the two variables by coercing the first value to an integer, assign
# that value to a variable, and display the value
# 4. add the two variables by coercing the second value to a string, assign
# that value to a variable, and display the value
# 5. create an array which holds the previous values
# 6. use a `for` loop to display each value in the array
# 7. add your name to the end of the array
# 8. use `each` to iterate through each value in the array, display the value
# 9. display the value with your name
# 10. add the string "example" to the beginning of the array
# 11. display the value with "example"
# 12. use `each` to iterate through each value in the array, display each value
# along with the data type. Example: "Value: example - Type: String"
# following: "Value: "
# 13. assign your name to a variable
# 14. assign your favorite color to a variable
# 15. create a hash with the keys "name" and "color" with the values from the
# variables above
# 16. using values from the hash above, display the following:
# "My name is Phil and my favorite color is Green."
# 17. create another hash with values for a friend of yours
# 18. create a new array using the people hashes above as values
# 19. iterate through the array of people, display the following for each:
# "My name is X and my favorite color is Y."
# 1. assign the string "5" to a variable and display the value
a = "5"
puts a
# 2. assign the integer 5 to a variable and display the value
b = 5
puts b
# 3. add the two variables by coercing the first value to an integer, assign
# that value to a variable, and display the value
c = a.to_i + b
puts c
# 4. add the two variables by coercing the second value to a string, assign
# that value to a variable, and display the value
d = a + b.to_s
puts d
# 5. create an array which holds the previous values
array = [a, b, c, d]
puts array
# 6. use a `for` loop to display each value in the array
for item in array
puts item
end
# 7. add your name to the end of the array
array[4] = "Phil"
# or
# array.push("Phil")
# or
# array << "Phil"
# 8. use `each` to iterate through each value in the array, display the value
array.each do |item|
puts item
end
# 9. display the value with your name
puts array[4]
# 10. add the string "example" to the beginning of the array
array.unshift("example")
# 11. display the value with "example"
puts array[0]
# 12. use `each` to iterate through each value in the array, display each value
# along with the data type. Example: "Value: example - Type: String"
# following: "Value: "
array.each do |item|
puts puts "Value: #{item} - Type: #{item.class}"
end
# 13. assign your name to a variable
name = "Phil"
# 14. assign your favorite color to a variable
color = "Green"
# 15. create a hash with the keys "name" and "color" with the values from the
# variables above
phil = {
"name" => name,
"color" => color
}
# 16. using values from the hash above, display the following:
# "My name is Phil and my favorite color is Green."
phrase = "My name is #{phil["name"]} and my favorite color is #{phil["color"]}."
puts phrase
# 17. create another hash with values for a friend of yours
don = {
"name" => "Don",
"color" => "Pink"
}
# 18. create a new array using the people hashes above as values
people = [phil, don]
# 19. iterate through the array of people, display the following for each:
# "My name is X and my favorite color is Y."
people.each do |person|
puts "My name is #{person["name"]} and my favorite color is #{person["color"]}."
end
# Remember: use `ruby exercise3.rb` to run this file
# 1. display the contents of the `my_variable` variable
my_variable = "100"
# 2. assign the integer 5 to a variable named 'number'
# 3. display the contents of the 'number' variable
# 4. change (coerce) the string in 'my_variable' into an integer
# 5. put the coerced string, now an integer, into a variable named 'new_number'
# 6. display the contents of the 'new_number' variable
# 7. add 'number' to 'new_number' and assign it to a variable named 'sum'
# 8. create an array with the values 1, 2, 4, and 5, assign it to a variable named `numbers`
# 9. display the contents of the array at index 1
# 10. display array element (contents) at index 10
# 11. use `each_with_index` to display the number and index for the `numbers` array
# 12. put a comment here that says what kind of operation `each_with_index` is
# Hint: comments start with a `#`
# 13. create a hash with the keys of one, two, three, four, five and the values of 1, 2, 3, 4, 5
# Hint: "one" => 1
# 14. use `each` to display the key and value for each member of the hash
# Question: did you remember to put the hash in a variable?
# 15. change the display of the hash to say:
# "The value of 'one' is 1" for each member of the hash, so 'two' is 2, and 'three' is 3, etc.
# Remember: use `ruby exercise3.rb` to run this file
# 1. display the contents of the `my_variable` variable
puts "# 1. display the contents of the `my_variable` variable"
my_variable = "100"
puts my_variable
# 2. assign the integer 5 to a variable named 'number'
puts "# 2. assign the integer 5 to a variable named 'number'"
number = 5
# 3. display the contents of the 'number' variable
puts "# 3. display the contents of the 'number' variable"
puts number
# or
print number
# or
puts number.inspect
# 4. change (coerce) the string in 'my_variable' into an integer
puts "# 4. change (coerce) the string in 'my_variable' into an integer"
puts my_variable.to_i
# 5. put the coerced string, now an integer, into a variable named 'new_number'
puts "# 5. put the coerced string, now an integer, into a variable named 'new_number'"
new_number = my_variable.to_i
# 6. display the contents of the 'new_number' variable
puts "# 6. display the contents of the 'new_number' variable"
puts new_number
# 7. add 'number' to 'new_number' and assign it to a variable named 'sum'
puts "# 7. add 'number' to 'new_number' and assign it to a variable named 'sum'"
result = number + new_number + (5 - 7) * 9
puts result
# 8. create an array with the values 1, 2, 4, and 5, assign it to a variable named `numbers`
puts "# 8. create an array with the values 1, 2, 4, and 5, assign it to a variable named `numbers`"
numbers = [1,2,3,4,5]
# 9. display the contents of the array at index 1
puts "# 9. display the contents of the array at index 1"
puts numbers[1]
# 10. display array element (contents) at index 10
puts "# 10. display array element (contents) at index 10"
puts numbers[10].inspect
# 11. use `each_with_index` to display the number and index for the `numbers` array
puts "# 11. use `each_with_index` to display the number and index for the `numbers` array"
numbers.each_with_index do |number, index|
puts number, index # Remember you can give `puts` more than one thing
end
# 12. put a comment here that says what kind of operation `each_with_index` is
# Hint: comments start with a `#`
# iteration or looping
puts "# 12. put a comment here that says what kind of operation `each_with_index` is"
puts "# interation or looping"
# 13. create a hash with the keys of one, two, three, four, five and the values of 1, 2, 3, 4, 5
# Hint: "one" => 1
puts "# 13. create a hash with the keys of one, two, three, four, five and the values of 1, 2, 3, 4, 5"
number_hash = {
"one" => 1,
"two" => 2,
"three" => 3,
"four" => 4,
"five" => 5
}
# 14. use `each` to display the key and value for each member of the hash
# Question: did you remember to put the hash in a variable?
puts "# 14. use `each` to display the key and value for each member of the hash"
number_hash.each do |key, value|
puts key, value
end
# 15. change the display of the hash to say:
# "The value of 'one' is 1" for each member of the hash, so 'two' is 2, and 'three' is 3, etc.
puts "# 15. change the display of the hash to say:"
number_hash.each do |key, value|
puts "The value of '#{key}' is #{value + 10}"
end
###############################################################################
# After working through the exercises we discussed other topics like:
## Variable Scope
puts "## Variable Scope"
# A variable created outside of the iteration block is available inside the
# block and after the block.
puts save_me = nil
numbers.each_with_index do |number, i|
save_me = number # assign the var here
puts "save_me is #{save_me}"
end
# save_me is availble here ... what is the value?
puts save_me
## Names of variables inside the pipes are arbitrary ...
puts "## Names of variables inside the pipes are arbitrary ..."
numbers.each_with_index do |a, n| # a & n instead of number & i
puts a, n
end
# But it's a good idea to use names that are meaningful for the context ...
# Like name and index if the array is an array of names
names = ["Phil", "Don"]
names.each_with_index do |name, index|
puts name, index
end
## Using different values for the key for a hash and the alignment of code.
# We could have made the keys for our hash the numbers instead of the text.
number_hash = {
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
11 => "eleven",
400 => "fourhundred"
}
## Single quoted vs. Double quoted strings
puts "## Single quoted vs. Double quoted strings"
foo = "FOO"
# Single quotes don't allow interpolation.
name = '"Don #{foo}"'
puts name
# Double quotes allow interpolation.
name = "Don #{foo}"
puts name
puts name.inspect
# If we need to have interpolation and double quotes *in* the string we can use
# an arbitrary string delimeter like %()
last_name = "Cohen"
other_name = %("Phil \#{last_name}")
puts other_name
puts other_name.inspect
numbers = [1,2,3,4,5]
save_me = nil
puts "save_me is #{save_me.inspect}"
numbers.each_with_index do |number, i|
save_me = number
puts "number is #{number}, index is #{i}"
puts "save_me is #{save_me}"
end
puts "save_me is #{save_me}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment