Skip to content

Instantly share code, notes, and snippets.

View PamBWillenz's full-sized avatar

PamBWillenz

View GitHub Profile
@PamBWillenz
PamBWillenz / Math Operations
Last active August 29, 2015 14:18
Basic Ruby Syntax
Strings are declared by placing quotes around a word. "Hello World"
A command is needed to instruct the computer to do something with the "hello world" string. As we mentioned in the checkpoint, Ruby has a command called p which prints the value of an object - p "hello world"
Booleans, Symbols and Variables - primitive data, just strings and numbers
Underscores make variables easier to read. When underscores are used in this capacity it's known as using "snake-case". The other type of case that you'll see in programming is "camel-case", which looks like: ThisIsCamelCase.
Symbols are like strings, only with less functionality. A Symbol is designated with a colon (:) to the left of a word. Symbols can contain any alpha-numeric character, but they must start with a letter.
@PamBWillenz
PamBWillenz / RSpec code
Last active August 29, 2015 14:18
Reading RSpec Tests
describe "greet" do
it "says hello to someone" do
greeting = greet("Steve", "Jobs")
expect(greeting).to eq("Hello Steve Jobs.")
end
end
In order to make this test pass, you would implement the greet method as it's written below:
def greet(first, last)
@PamBWillenz
PamBWillenz / No Method Error
Created April 8, 2015 13:21
Debugging Code
def hello(name1)
"Hello #{name1}"
end
def hello(name2)
"Hello #{name2}"
end
p hello("World")
p hello("Bob")
def hello(firstpresident1, lastpresident1)
"Hello #{firstpresident1} #{lastpresident1}"
end
def hello(firstpresident2, lastpresident2)
"Hello #{firstpresident2} #{lastpresident2}"
end
p hello("Abraham", "Lincoln")
p hello("George", "Washington")
Output
@PamBWillenz
PamBWillenz / No Name Error
Last active August 29, 2015 14:18
Debugging Code
def hello(first, last)
"Hello #{first} #{last}"
end
p hello("Steve", "Jobs")
Output
"Hello Steve Jobs"
# describe "hello" do
# it "should return 'Hello first name last name'" do
@PamBWillenz
PamBWillenz / Type Error
Last active August 29, 2015 14:18
Debugging Code
# a + " + " + b + " = " + (a + b)
def add(a,b)
"#{a} + #{b} = #{a+b}"
end
p add(1,2)
p add(5,7)
p add(10,12)
@PamBWillenz
PamBWillenz / Unexpected End
Created April 8, 2015 16:40
Debugging Code
def hello
"Hello World"
end
# describe "hello" do
# it "should return 'Hello World'" do
# expect(hello).to eq("Hello World")
# end
@PamBWillenz
PamBWillenz / And_ Or
Last active August 29, 2015 14:18
If Statements- Bloc Checkpoint
Specs
LOCK METHOD
The lock method should take four arguments:
def lock(a,b,c,d)
end
Each argument represents a number on a combination lock. The method will return "unlocked" or "locked", depending on whether the combination is correct. There are several combinations that are "correct".
The first number has to be a 3, 5, or 7
The second number has to be a 2
@PamBWillenz
PamBWillenz / Array Definition
Last active August 29, 2015 14:18
Arrays - Code Checkpoint
def new_array(a,b,c,d)
# return an array consisting of the arguments here
end
SPECS
describe "new_array" do
it "creates an array of numbers" do
expect( new_array(1,2,3,4) ).to eq([1,2,3,4])
end
it "creates an array of strings" do
expect( new_array("a", "b", "c", "d") ).to eq(["a", "b", "c", "d"])
@PamBWillenz
PamBWillenz / Attr Accessor
Last active August 29, 2015 14:18
INTRO TO CLASSES - Bloc Checkpoint
Getter and setter methods are so common that Ruby provides a shortcut to create them. The attr_accessor method creates a getter and setter method based on an argument. You call the attr_accessor with a Symbol argument:
class Square
attr_accessor :size
end
s = Square.new
s.size = 10 # This is the setter
s.size #=> 10 # This is the getter
Notice how we have both the getter and setter methods for size, because we called: