Skip to content

Instantly share code, notes, and snippets.

@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: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"
module Calculator
def add(num)
self + num
end
def subtract(num)
self - num
end
def multiply_by(num)
@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)
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
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 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
@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
@kmandreza
kmandreza / gist:2985424
Created June 24, 2012 23:09
Refactored ToDo
doc = "Usage: example.rb [options] <arguments>...
Options:
-h --help show this help message and exit
add add task to the end of the list
prepend add task to the beginning of list"
require 'docopt'
if __FILE__ == $0
@kmandreza
kmandreza / evolution.rb
Created June 24, 2012 05:40
Evolution....
#TO CREATE A NEW CLASS
module EvolutionarySkills
def breed_rapidly(howrapid)
puts cross_mate.*(howrapid)
end
end
class SomeNewAnimal < String
include EvolutionarySkills