Skip to content

Instantly share code, notes, and snippets.

View randalmaile's full-sized avatar

Randal Maile randalmaile

View GitHub Profile
@randalmaile
randalmaile / Ruby Syntax Checkpoint
Last active December 30, 2015 10:49
Ruby Syntax ckpt
# 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"
@randalmaile
randalmaile / Reading RSpec Tests checkpoint
Last active December 30, 2015 15:59
Reading RSpec Tests checkpoint
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>")
@randalmaile
randalmaile / Debugging Code Checkpoint
Last active December 30, 2015 16:08
Debugging Code Checkpoint
# 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")
@randalmaile
randalmaile / If Statements Checkpoint
Last active December 30, 2015 16:09
If Statements checkpoint
#IF STATEMENTS
def favorite_number(fav, guess)
if guess < fav
"Too low"
elsif guess > fav
"Too high"
else
"You got it!"
end
@randalmaile
randalmaile / Arrays Checkpoint
Last active December 30, 2015 17:59
Arrays Checkpoint
# 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
@randalmaile
randalmaile / Intro to Classes checkpoint
Last active December 30, 2015 18:09
Intro to Classes checkpoint
# Basics
class Book
def title_and_author(title, author)
# set title to instance variable
# set author to instance variable
@title = title
@author = author
end
@randalmaile
randalmaile / Intro to Classes 2 checkpoint
Last active December 30, 2015 18:18
Intro to Classes 2 checkpoint
# INITIALIZE
class Car
attr_accessor :make
attr_accessor :model
attr_accessor :year
def initialize(make, model, year)
@make, @model, @year = make, model, year
end
@randalmaile
randalmaile / Loops checkpoint
Last active December 30, 2015 18:19
Loops checkpoint
# 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"
@randalmaile
randalmaile / Hashes Checkpoint
Last active December 30, 2015 20:49
Hashes Checkpoint
#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"
@randalmaile
randalmaile / Advanced Classes checkpoint
Last active December 30, 2015 22:39
Advanced Classes checkpoint
#Inheritance
class Shape
attr_accessor :color
def initialize(color = nil)
@color = color || "Red"
end
def can_fit?(shapeInstance)
if self.area >= shapeInstance.area