Skip to content

Instantly share code, notes, and snippets.

@kmandreza
kmandreza / gist:2992328
Created June 26, 2012 00:33
Re-refactored ToDo
class List
def initialize(title)
@list = []
@title = title
end
def append(item)
if item.is_a?(Item)
@list << item
def translate(word)
if word[0] == "a" || word[0] == "e" || word[0] == "i" || word[0] == "o" || word[0] == "u"
word << "ay"
elsif word[0..1] == "qu"
f = (word[0..1] << 'ay')
word[0..1] = ""
word << f
def triangle(num1,num2,num3)
if num1 >= num2 + num3 || num2 >= num1 + num3 || num3 >= num2 + num1
:invalid
elsif num1 == num2 && num2 == num3 && num3 == num1
:equilateral
elsif num1 == num2 || num2 == num3 || num3 == num1
:isosceles
elsif num1 != num2 && num2 != num3 && num3 != num1
:scalene
end
def smiley(input)
if input[:mood] == "happy" #mood is the key, it also has to be a string
return ":)"
elsif input[:mood] == "sad"
return ":("
else
return ":|"
end
end
@kmandreza
kmandreza / gist:3026453
Created July 1, 2012 01:45
Instance Method Calculator
class Numeric
def add(num)
self + num
end
def subtract(num)
self - num
end
def multiply(num)
module Calculator
def add(num)
self + num
end
def subtract(num)
self - num
end
def multiply_by(num)
@kmandreza
kmandreza / gist:3140484
Created July 19, 2012 03:02
fun_string!
Exercise 4
Create a new instance method on the String class called fun_string! which is called on a String object and permanently modifies it by capitalizing the even characters (counting from 1) and then reversing the string. See if you can do it in a single line.
For example,
string = "apples"
string.fun_string!
string # now equals "SeLpPa"
@kmandreza
kmandreza / gist:3556822
Created August 31, 2012 18:15
8.31.12 SpaceShip class
#Object Oriented Design, Simon Allardice from lynda.com
#Class: SpaceShip
#Attributes: +public name: String
# -private shieldstrength: Integer
#Operations: +fire( ): String
# -reduce shields (Integer)
#Notes: 1. Public Class Spaceship
# 2. Attributes are expressed as instance variables, which are variables that belong to an instance of a class. What this means is that any object created within this class will have their own copy of these variables <--I NEED A VISUAL OF THIS BADLY
@kmandreza
kmandreza / gist:3612955
Created September 3, 2012 20:05
Sort an Array
#Given a random array, create a method that sorts the array so the numbers are returned in order.
a= [5, 8, 3, 2, 7, 4, 1, 0, 9, 6, 10, 55]
#Final Answer
def singlesort(a)
(a.size - 1).times do |i|
if a[i] > a[i+1]
x=a[i]
a[i] = a[i+1]
@kmandreza
kmandreza / Define a method that returns a formatted address
Created December 23, 2012 05:35
Exercise: Define a method that returns a formatted address Create a method called make_address that accepts parameters for the street, city , state, and zip and returns a single address string. For example if we call the method as follows: make_address("717 California St.", "San Francisco", "CA", "94111") The return value would be "You live at 7…
def make_address(street,city,state,zip)
"You live at #{street} in the beautiful city of #{city}, #{state}. Your zip is #{zip}."
end
make_address("6056 Plumas St.", "Reno", "NV","89519")