This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Calculator | |
def add(num) | |
self + num | |
end | |
def subtract(num) | |
self - num | |
end | |
def multiply_by(num) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Numeric | |
def add(num) | |
self + num | |
end | |
def subtract(num) | |
self - num | |
end | |
def multiply(num) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class List | |
def initialize(title) | |
@list = [] | |
@title = title | |
end | |
def append(item) | |
if item.is_a?(Item) | |
@list << item |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#TO CREATE A NEW CLASS | |
module EvolutionarySkills | |
def breed_rapidly(howrapid) | |
puts cross_mate.*(howrapid) | |
end | |
end | |
class SomeNewAnimal < String | |
include EvolutionarySkills |