Created
October 12, 2012 03:04
-
-
Save adamjonas/3877066 to your computer and use it in GitHub Desktop.
Friday Oct 12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assignment | |
# write a true expression using == | |
a == a | |
# write a false expression using == | |
a == b | |
# write a true expression using != | |
true != false | |
# write a false expression using != | |
# Write an if statement with 3 different branches | |
# use !=, ==, and > in those branches | |
if x != 1 | |
puts "it's not one" | |
elsif x == 2 | |
puts "it's two" | |
elseif x > 3 | |
puts "it's greater than three" | |
end | |
# Assign a variable based on the result of an if statement | |
# Execute code based on the result of an if statement. | |
# conditionally run puts "Hello Class" if 1 < 2 | |
# Try using an if statement at the end of an expression | |
x = "Hello class" if 1 < 2 | |
puts x | |
# Write an if statement that negates the or condition to create a true return | |
if x == 1 || x == 2 | |
puts "it's either one or two" | |
end | |
# Write an if statement that users the and operator to create a false return | |
if x > 10 && x < 15 | |
puts "it's between 10 and 15" | |
end | |
# Write a Case Statement that checks if a variable is a vowel | |
vowel = [a, e, i, o, u] | |
input == gets.chomp | |
case letter | |
when vowel.includes?(input) | |
puts "this is a vowel" | |
else | |
puts "this isn't a vowel" | |
end | |
end | |
# Write that same case as an if statement | |
# Write a Case statement that has at 4 branches and a default return | |
vowel = [a, e, i, o, u] | |
letter = gets.chomp | |
if letter == a | |
puts "this is a vowel" | |
elsif letter == e | |
puts "this is a vowel" | |
elsif letter == i | |
puts "this is a vowel" | |
elsif letter == o | |
puts "this is a vowel" | |
elsif letter == u | |
puts "this is a vowel" | |
else | |
puts "this is not a vowel" | |
end | |
# Assignment | |
# Write a while loop that runs exactly 5 times | |
5.times do | |
puts "hello nurse" | |
end | |
# Write a while loop that counts from 1 to 10 and puts all odd numbers | |
# => you can check if a number is odd by calling the odd? method on it. | |
# => 1.odd? will return true | |
while x < 10 | |
(1..10).each do |x| | |
puts x if x.odd? | |
end | |
end | |
success = "this is a vowel" | |
# Write a Case Statement that checks if a variable is a vowel | |
print "Enter your variable to check: " | |
variable = gets.chomp | |
variable = variable.downcase | |
case | |
when variable.match(/[aeiou]/) | |
puts success | |
else | |
puts "this is not a vowel" | |
end | |
# Write that same case as an if statement | |
success = "this is a vowel" | |
def check_var(var) | |
"#{var.downcase}".each do |letter| | |
if letter == "a" | |
print "A" | |
elsif letter == "e" | |
print "E" | |
elsif letter == "i" | |
print "I" | |
elsif letter == "o" | |
print "O" | |
elsif letter == "u" | |
print "U" | |
else | |
print "no vowels" | |
end | |
end | |
end | |
# Write a Case statement that has at 4 branches and a default return | |
print "Enter your variable to check: " | |
variable = gets.chomp | |
variable = variable.downcase | |
case variable | |
when "hi" | |
puts "hi to you too" | |
when "e" | |
puts "eeee" | |
when "i" | |
puts "there is no I in team, but their is an I in win" | |
when "o" | |
puts "oooo la la" | |
else | |
puts "why don't you type hi, e, i, or o instead?" | |
end | |
# Write a while loop that runs exactly 5 times | |
5.times do puts "hello nurse" end | |
# Write a while loop that counts from 1 to 10 and puts all odd numbers | |
# => you can check if a number is odd by calling the odd? method on it. | |
# => 1.odd? will return true | |
(1..10).each do |num| | |
puts num if num.odd? | |
end | |
# Construct an array with your favorite foods. It should have at least | |
# 5 elements. | |
fav_foods = ["lobster", "chicken", "pizza", "apples", "pears"] | |
# puts your most favorite food out of that array | |
puts fav_foods[2] | |
# Construct an array with the colors of the rainbow (ROYGBIV) | |
colors = ["red", "Orange", "Yellow", "green", "blue", "indigo", "violet"] | |
# Slice the colors Red, Orange, and Yellow out of the array | |
puts colors.slice(0,3) | |
# Start with an empty array and then fill the second and 6th position | |
empty = [] | |
empty[1] = "2nd" | |
empty[5] = "6th" | |
puts empty | |
# | |
# Create a variable that stores your name as a string. | |
name = "Adam" | |
# Send that variable (string object) the upcase method. | |
name = name.upcase | |
# Try to slice the middle letter of your name out of the string. | |
name= name.slice(2,2) | |
# http://ruby-doc.org/core-1.9.3/String.html#method-i-slice | |
puts name | |
# Chain at least 5 method calls to an object. | |
str = " chicken ".strip.upcase.slice(0,5).downcase.concat("s are cute") | |
# chicken | |
# CHICKEN | |
# CHICK | |
# chick | |
# Chicks are cute | |
puts str | |
# Reduce this operation one chain at a time. | |
# Example: | |
# Full Chain: | |
# puts "start".reverse.slice(0,2).concat("uth").capitalize | |
# trats | |
#tr | |
#truth | |
#Truth | |
# At any given point in the chain, there is an evaluation | |
# that then receives the next method call | |
# Try to use as many of the string methods as possible. | |
# Literally, try to write a ruby script that demonstrates the usage of | |
# every string method | |
# Iterate through all the letters in your name outputing all vowels. | |
def output_var(name) | |
"#{name}".each_char do |letter| | |
letter = letter.downcase | |
case | |
when letter.match(/[aeiou]/) | |
puts letter | |
end | |
end | |
end | |
output_var("Adam") | |
# Given this List of Songs, Construct Arrays by Artist and Album | |
# Hint: Make use of the split() String Method | |
# http://www.ruby-doc.org/core-1.9.3/String.html#method-i-split | |
# Simple Example of Data Parsing | |
songs = [ | |
"The Magnetic Fields - 69 Love Songs - Parades Go By", | |
"The Magnetic Fields - Get Lost - Smoke and Mirrors", | |
"The Magnetic Fields - Get Lost - You, Me, and the Moon", | |
"The Magnetic Fields - 69 Love Songs - The Book of Love", | |
"Neutral Milk Hotel - In An Aeroplane Over the Sea - Holland 1945", | |
"Neutral Milk Hotel - In An Aeroplane Over the Sea - The King of Carrot Flowers" | |
] | |
artist = [] | |
album = [] | |
tracks = [] | |
songs.each do |song| | |
piece = song.split("-") | |
artist << piece[0] | |
album << piece[1] | |
tracks << piece[2] | |
artist = artist.uniq | |
album = album.uniq | |
tracks = tracks.uniq | |
end | |
puts "These are the artists: #{artist}" | |
puts "These are the albums: #{album}" | |
puts "These are the songs: #{tracks}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment