Skip to content

Instantly share code, notes, and snippets.

@hchood
Last active August 29, 2015 13:56
Show Gist options
  • Save hchood/9190436 to your computer and use it in GitHub Desktop.
Save hchood/9190436 to your computer and use it in GitHub Desktop.
return_value_drills
# RUBY FUNDAMENTALS III: NON-CORE RETURN VALUE DRILLS
# ----------------------------
def is_a_number?(some_string)
if some_string =~ /\A\d*[.]{0,1}\d*\z/
true
else
false
end
end
is_a_number?("10")
is_a_number?("3.14159265359")
is_a_number?(".0002")
is_a_number?("cat")
# ----------------------------
def is_a_number?(some_string)
true if some_string =~ /\A\d*[.]{0,1}\d*\z/
end
is_a_number?("10")
is_a_number?("cat")
# What would happen if we called is_a_number?(10) ?
# ----------------------------
def is_a_fixnum?(some_input)
some_input.class == Fixnum
end
is_a_fixnum?(10)
is_a_fixnum?("10")
# ----------------------------
def is_a_26_year_old_coder_who_knows_ruby?(age, profession, languages)
age == 26 && profession == "coder" && languages.include?("Ruby")
end
is_a_26_year_old_coder_who_knows_ruby?(26, "coder", ["ruby", "Python", ".NET"])
is_a_26_year_old_coder_who_knows_ruby?("26", "coder", ["Ruby", "Python", ".NET"])
is_a_26_year_old_coder_who_knows_ruby?(26, "coder", ["Ruby", "Python", "C"])
is_a_26_year_old_coder_who_knows_ruby?(26, "coder", ["JavaScript", "C"])
# ----------------------------
def guesses_hidden_word(hidden_word, guess)
if guess == hidden_word
puts "Congrats! You win!"
else
puts "Sorry, you lose..."
end
end
guesses_hidden_word("hamster", "cat")
guesses_hidden_word("hamster", "hamster")
# ----------------------------
def guesses_hidden_word(hidden_word, guess)
if guess == hidden_word
"Congrats! You win!"
else
"Sorry, you lose..."
end
end
guesses_hidden_word("hamster", "cat")
guesses_hidden_word("hamster", "hamster")
# ----------------------------
"doge".join("coin")
["Dogecoins", "are", "stupid."].join(" ")
string = "yay, it's Friday!"
string[10..-1]
# ----------------------------
def remove_non_letters(string)
string.gsub(/[^a-z]/i, '')
end
remove_non_letters("7 Dwarves")
remove_non_letters("Hello world!")
# ----------------------------
string = "Some stringy string"
string.match("s")
string.match(/s/)
string.match("p")
# ----------------------------
array = [1, 7, "cat", "monkey", "doge", 3.14]
array.index(7)
array.index(0)
array.index("cat")
array[1] = "hello!"
array.index(7)
# ----------------------------
def numbers_squared(numbers)
nums_squared = []
numbers.each do |num|
num_squared = num * num
nums_squared << num_squared
end
end
numbers_squared([1, 2, 3, 4])
# ----------------------------
def numbers_squared(numbers)
nums_squared = []
numbers.each do |num|
num_squared = num * num
nums_squared << num_squared
end
nums_squared
end
numbers_squared([1, 2, 3, 4])
# ----------------------------
def upcase_names(names)
names.map { |name| name.upcase }
end
upcase_names(["George Michael", "Maeby", "Oscar"])
# ----------------------------
def upcase_names(names)
names.map { |name| name.upcase }
names
end
upcase_names(["George Michael", "Maeby", "Oscar"])
# ----------------------------
def upcase_names(names)
names.map! { |name| name.upcase }
names
end
upcase_names(["George Michael", "Maybe", "Oscar"])
# ----------------------------
def words_that_start_with_vowel(array_of_words)
vowel_words = array_of_words.select { |word| word.downcase.start_with?("a", "e", "i", "o", "u") }
vowel_words.sort_by { |word| word.downcase }
end
animals = ["slow loris", "Elephant", "Sloth", "orangutan", "anaconda", "Meer cat", "wart hog"]
words_that_start_with_vowel(animals)
# ----------------------------
def list_full_names(array_of_names)
array_of_names.sort_by! { |name| name[1].downcase }
full_names = array_of_names.map { |name| name.join(" ") }
full_names.join(", ")
end
def greets(array_of_names)
full_names = list_full_names(array_of_names).split(", ")
full_names.each do |full_name|
puts "Hi, #{full_name}"
end
end
character_names = [["Bob","Loblaw"], ["Gene", "Parmesan"], ["STEVE", "HOLT!"]]
list_full_names(character_names)
greets(character_names)
# ----------------------------
def is_a_number?(some_input)
true if some_input =~ /\A\d*[.]{0,1}\d*\z/
end
def extract_numbers_from(array)
array.flatten!
array.select { |element| is_a_number?(element) || element.is_a?(Numeric)}
end
array_of_random_stuff = [42, "alphabet", "octocat", 1.899, "25", "123", ["90.7", 84, "doge"]]
extract_numbers_from(array_of_random_stuff)
# ----------------------------
my_car = {
make: "Nissan",
model: "Maxima",
color: "White",
year: "1995"
}
my_car.merge({mileage: 110000, year: "1998" })
my_car[:year]
# ----------------------------
my_car = {
make: "Nissan",
model: "Maxima",
color: "White",
year: "1995"
}
my_car.merge!({mileage: 110000, year: "1998" })
my_car[:year]
# ----------------------------
people = [
{
first_name: "George",
middle_name: "Michael",
last_name: "Bluth",
gender: "Male",
employer: "Banana Stands, Inc."
},
{
first_name: "Michael",
last_name: "Bluth",
gender: "Male",
employer: "Sudden Valley Homes"
},
{
first_name: "GOB",
last_name: "Bluth",
gender: "Male",
},
{
first_name: "Maeby",
last_name: "Funke",
gender: "Female",
employer: "Warner Brothers"
}
]
def list_employers(people)
employers = []
people.each do |person|
employer_hash = {}
if person[:employer]
if person[:middle_name]
full_name = "#{person[:first_name]} #{person[:middle_name]} #{person[:last_name]}"
else
full_name = "#{person[:first_name]} #{person[:last_name]}"
end
employer_hash[:full_name] = full_name
employer_hash[:employer] = person[:employer]
employers << employer_hash
end
end
employers
end
list_employers(people)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment