Skip to content

Instantly share code, notes, and snippets.

2.2.1 :001 > def say_hi(name)
2.2.1 :002?> "Hi, #{name}"
2.2.1 :003?> end
=> :say_hi
2.2.1 :004 > say_hi("stu")
=> "Hi, stu"
2.2.1 :005 > my_array = [4, 7, 1, 0]
=> [4, 7, 1, 0]
2.2.1 :006 > my_array.sort
=> [0, 1, 4, 7]
# # Find the maximum
# def maximum(arr)
# arr.max
# end
def number(arr)
a = "".to_i
arr.each do |b|
if a < b
a = b
def fizzbuzz(a, b)
a.upto(b) do |i|
if i % 5 == 0 && i % 3 == 0
puts "FizzBuzz"
elsif i % 5 == 0
puts "Buzz"
elsif i % 3 == 0
puts "Fizz"
else
# must be baller and either furnished or rent cheaper than 2100
def rent?(baller, furnished, rent)
if (baller && furnished) || rent < 2100
puts true
else
puts false
end
end
###
# Save this file to your computer so you can run it
# via the command line (Terminal) like so:
# $ ruby shakil_the_dog.rb
#
# Your method should wait for user input, which corresponds
# to you saying something to your dog (named Shakil).
# You'll probably want to write other methods, but this
# encapsulates the core dog logic
# def shakil_the_dog
# Sort the array from lowest to highest
def sort(arr)
n = arr.length
loop do
swapped = false
(n-1).times do |i|
if arr[i] > arr[i+1]
arr[i], arr[i+1] = arr[i+1], arr[i]
swapped = true
def sign_order
puts "How many square feet is your sign?"
size = gets.chomp.to_i
subtotal_1 = size * 15
puts "How many colours would you like?"
colours = gets.chomp.to_i
if colours <= 2
subtotal_2 = colours * 10
else
subtotal_2 = colours * 15
1.
list = {'yvr' => 'Vancouver', 'yba' => 'Banff', 'yyz' => 'Toronto', 'yxx' => 'Abbotsford', 'ybw' => 'Calgary'}
# Why is it returning nil instead of first element of the list above
p list['yvr']
2.
def average(numbers)
sum = 0
numbers.each do |num|
require 'pry'
def count_letters(string)
letter_hash = Hash.new(0)
# string.each do |chars|
string.split('').each do |i|
letter_hash[i] += 1
end
# end
letter_hash
require 'pry'
@states = {
OR: 'Oregon',
FL: 'Florida',
CA: 'California',
NY: 'New York',
MI: 'Michigan'
}