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
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. |
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
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) |
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 hello(name1) | |
"Hello #{name1}" | |
end | |
def hello(name2) | |
"Hello #{name2}" | |
end | |
p hello("World") | |
p hello("Bob") |
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 hello(firstpresident1, lastpresident1) | |
"Hello #{firstpresident1} #{lastpresident1}" | |
end | |
def hello(firstpresident2, lastpresident2) | |
"Hello #{firstpresident2} #{lastpresident2}" | |
end | |
p hello("Abraham", "Lincoln") | |
p hello("George", "Washington") | |
Output |
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 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 |
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
# 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) |
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 hello | |
"Hello World" | |
end | |
# describe "hello" do | |
# it "should return 'Hello World'" do | |
# expect(hello).to eq("Hello World") | |
# 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
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 |
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 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"]) |
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
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: |
OlderNewer