Skip to content

Instantly share code, notes, and snippets.

vagrant [coding_ruby]> irb
2.1.3 :001 > quit
vagrant [coding_ruby]> pry
[1] pry(main)> def say_hi
[1] pry(main)* puts "input your name"
[1] pry(main)* name = gets.chomp
[1] pry(main)* puts "hi #{name}"
[1] pry(main)* end
=> :say_hi
[2] pry(main)> say_hi
# Find the maximum
def maximum(arr = 0)
#arr.max #Initial code (lighthouse code)
max_value = arr[0]
max_value = 0 if arr[0] == nil #Avoid empty array, makes answer zero
arr.each do |i|
max_value = i if (max_value <= i)
end
@Andsbf
Andsbf / gist:464e9486260374f34899
Created March 3, 2015 22:13
FizzBuzz Refactor Exercise
def fizzBuzz(_start,_end)
_start.upto(_end) {|i|
puts "Fizz" if i%3 == 0 && i%5 != 0
puts "Buzz" if i%3 != 0 && i%5 == 0
puts "FizzBuzz" if i%3 == 0 && i%5 == 0
puts i if i%3 != 0 && i%5 != 0
}
end
@Andsbf
Andsbf / The Yuppie Vancouverite
Created March 4, 2015 00:30
The Yuppie Vancouverite Exercise
# must be baller and either furnished or rent cheaper than 2100
def rent?(furnished, baller, rent)
baller && (furnished || rent < 2100)
# if baller && (furnished || rent < 2100)
# return puts true
# else
# return puts false
# end
end
@Andsbf
Andsbf / Shakil The Dog
Created March 4, 2015 01:43
Exercise Shakil The Dog
def user_input
puts "what do you have to say to Shakil(dog):"
gets.chomp
end
def shakil_behavior(user_input)
user_input = "treat" if user_input.include? "treat"
case user_input
@Andsbf
Andsbf / Personal Sort array + benchmark test
Last active August 29, 2015 14:16
Sort array (Bubble sort) Exercise
# # Sort the array from lowest to highest
def sort_Anderson(arr)
# arr.sort #initial code
flag =true
while flag
arr_before = arr.clone
arr = check_next(arr)
flag = false if arr_before == arr
end
@Andsbf
Andsbf / Sign shop exercise
Created March 4, 2015 04:37
Sign shop app
def get_dimension_price
while true
puts "Input banner width in m:"
width = gets.chomp.to_f
break if (width != 0.0 && width < 150) # avoid string input and unreasonable values=a banner bigger than 150m
end
puts "Input banner height in m:"
height = gets.chomp.to_f
@Andsbf
Andsbf / debug01.rb
Created March 4, 2015 20:30
Debug Exercise
require 'pry'
list = {'yvr' => 'Vancouver', 'yba' => 'Banff', 'yyz' => 'Toronto', 'yxx' => 'Abbotsford', 'ybw' => 'Calgary'}
binding.pry
# Why is it returning nil instead of first element of the list above
p list.first
@Andsbf
Andsbf / count_letters.rb
Last active August 29, 2015 14:16
Character Counting Exercise
require 'pry'
def count_letters (string="")
counts = Hash.new(0)
string.split("").each do |element|
# binding.pry
counts[element] += 1 if element != " "
end
@Andsbf
Andsbf / state_info.rb
Created March 5, 2015 00:51
States & Cities Exercise
# require 'pry'
@states = {
OR: 'Oregon',
FL: 'Florida',
CA: 'California',
NY: 'New York',
MI: 'Michigan'
}
@states[:TX] = "Texas"
@states[:OK] = "OKlahoma"