This file contains 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
# Math Operations | |
p "simple math" | |
p 1+2 | |
p 5-1 | |
p 6/2 | |
p 4*3 | |
p "exponents" | |
p 2**3 | |
p "operations run on variables" |
This file contains 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 link_to(text, address) | |
p "<a href='#{address}'>#{text}</a>" | |
end | |
describe "link_to" do | |
it "should return a valid link for Bloc" do | |
link_to("Bloc", "http://www.bloc.io").should eq("<a href='http://www.bloc.io'>Bloc</a>") | |
end | |
it "should return a valid link for Google" do | |
link_to("Google", "http://www.google.com").should eq("<a href='http://www.google.com'>Google</a>") |
This file contains 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
# NO METHOD ERROR | |
def hello(name) | |
p "Hello #{name}" | |
end | |
describe "hello" do | |
it "should return 'Hello World' when passed 'World'" do | |
hello("World").should eq("Hello World") |
This file contains 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
#IF STATEMENTS | |
def favorite_number(fav, guess) | |
if guess < fav | |
"Too low" | |
elsif guess > fav | |
"Too high" | |
else | |
"You got it!" | |
end |
This file contains 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
# ARRAY DEFINITION | |
def new_array(a,b,c,d) | |
#return an array consiting of the arguments here | |
$new_array = [a,b,c,d] | |
end | |
def first_and_last(a) | |
$first_and_last = [a.first, a.last] | |
end |
This file contains 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
# Basics | |
class Book | |
def title_and_author(title, author) | |
# set title to instance variable | |
# set author to instance variable | |
@title = title | |
@author = author | |
end | |
This file contains 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
# INITIALIZE | |
class Car | |
attr_accessor :make | |
attr_accessor :model | |
attr_accessor :year | |
def initialize(make, model, year) | |
@make, @model, @year = make, model, year | |
end |
This file contains 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
# Each loop (non-assignment practice) | |
[1] pry(main)> a = ["red", "blue", "green"] | |
=> ["red", "blue", "green"] | |
[2] pry(main)> a.each do |color| | |
[2] pry(main)* p "The color is #{color}" | |
[2] pry(main)* end | |
"The color is red" | |
"The color is blue" | |
"The color is green" |
This file contains 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
#SETTING ATTRIBUTES | |
class User | |
attr_accessor :name, :email, :bio, :age, :sex | |
def initialize(config = {}) | |
@name = config[:name] || "n/a" | |
@email = config[:email] || "n/a" | |
@bio = config[:bio] || "n/a" | |
@age = config[:age] || "n/a" | |
@sex = config[:sex] || "n/a" |
This file contains 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
#Inheritance | |
class Shape | |
attr_accessor :color | |
def initialize(color = nil) | |
@color = color || "Red" | |
end | |
def can_fit?(shapeInstance) | |
if self.area >= shapeInstance.area |
OlderNewer