Skip to content

Instantly share code, notes, and snippets.

View cody-code-wy's full-sized avatar

William Young cody-code-wy

View GitHub Profile
def cost(length,width,colors)
x = length * width;
if colors > 2
x += colors * 15
else
x += colors * 10
end
return x * 1.15
end
def mergesort(array)
if array.count <= 1
return array #Sorted!
end
half = array.count / 2
a = mergesort(array.slice(0,half))
b = mergesort(array.slice(half,array.count - half))
array = [] #Empty it to start sortinng
def mergesort(array)
if array.count <= 1
return array #Sorted!
end
half = array.count / 2
a = mergesort(array.slice(0,half))
b = mergesort(array.slice(half,array.count - half))
array = [] #Empty it to start sortinng
def count_letters(letter)
hash = Hash.new { |hash,key| hash[key] = [] }
letter.split("").each_with_index() { |x,i|
hash[x] << i
}
hash
end
@ids = [[1,'I'],[5,'V'],[10,'X'],[50,'L'],[100,'C'],[500,'D'],[1000,'M']]
def to_roman(num)
str = ''
@ids.reverse.each { |id|
until num / id[0] == 0
str += id[1]
num -= id[0]
end
if num == id[0]-1 && check[0] != 1
#takes a dollar ammount and return the number of purchased bottles plus the maximum number of bottles possible with recyling
def total_bottles_from_purchase(amt)
amt = amt / 2 #get the number of purchased bottles
#Track the bottles, and caps not redeemed. Track the number of purchased, and free bottles recieved
tracker = {bottles:amt,caps:amt,purchased:amt,free:0}
while (tracker[:bottles] >= 2) || (tracker[:caps] >= 4)
#get the number of new free bottles from recycling
free = tracker[:bottles] / 2
free += tracker[:caps] / 4
#remove reedemed bottles and caps
@states = {
OR: {name:'Oregon',city:["Portland","Bend"],tax:1},
FL: {name:'Florida',city:["Orlando","Miami"],tax:0.9},
CA: {name: 'California',city:["Los Angeles"],tax:0.2},
NY: {name:'New York',city:["New York","Albany"],tax:0},
MI: {name:'Michigan',city:["Ann Arbor","Jackson"],tax:0.4}
}
@states[:TX] = {name:"Texas",city:["Dallas","Huston","Austen"],tax:8}
@states[:WA] = {name:"Washington",city:["Spokan","Seattle"],tax:0.1}
module Flight
def fly
puts "I'm a #{self.class.name}, I'm flying!"
end
end
class Animal
@name = "Animal"

The self keyword

Self in instance methods

Self referst to the instance of a class it is evoked from. Or in other words, it allows you to address methods on its own instance. This feature is not normally required in ruby. It is most usefull when a method's name is ambiguous between instance methods, and other methods that are in scope.

Defining class methods

Self can also be used to define class methods, for example