Skip to content

Commit

Permalink
Finished methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nagorevalero committed Mar 8, 2022
1 parent 8e5df5a commit 86602c3
Show file tree
Hide file tree
Showing 20 changed files with 223 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions lib/check_codeword.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def check_codeword(codeword)
if codeword == "horse"
return "Correct! Come in."
elsif codeword.chars.first == "h" && codeword.chars.last == "e"
return "Close, but nope."
else
return "WRONG!"
end
end
13 changes: 13 additions & 0 deletions lib/counter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Counter
def initialize
@count = 0
end

def add(num)
@count += num
end

def report
return "Counted to #{@count} so far."
end
end
15 changes: 15 additions & 0 deletions lib/gratitudes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Gratitudes
def initialize
@gratitudes = []
end

def add(gratitude)
@gratitudes.push(gratitude)
end

def format
formatted = "Be grateful for: "
formatted += @gratitudes.join(", ")
end
end

5 changes: 5 additions & 0 deletions lib/greet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def greet(name)
return "Hello, #{name}!"
end


9 changes: 9 additions & 0 deletions lib/password_checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class PasswordChecker
def check(password)
if password.length >= 8
return true
else
fail "Invalid password, must be 8+ characters."
end
end
end
21 changes: 21 additions & 0 deletions lib/present.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Present
def wrap(contents)
fail "A contents has already been wrapped." unless @contents.nil?
@contents = contents
end

def unwrap
fail "No contents have been wrapped." if @contents.nil?
return @contents
end
end

# require 'reminder'
# RSpec.describe Reminder do
# context "when no task is set" do
# it "fails" do
# reminder = Reminder.new("Kay")
# expect { reminder.remind() }.to raise_error "No reminder set!"
# end
# end
# end
13 changes: 13 additions & 0 deletions lib/reminder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Reminder
def initialize(name)
@name = name
end

def remind_me_to(task)
@task = task
end

def remind()
return "#{@task}, #{@name}!"
end
end
4 changes: 4 additions & 0 deletions lib/report_length.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def report_length(str)
length = str.length
return "This string was #{length} characters long."
end
17 changes: 17 additions & 0 deletions lib/string_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class StringBuilder
def initialize
@str = ""
end

def add(str)
@str += str
end

def size
return @str.length
end

def output
return @str
end
end
19 changes: 19 additions & 0 deletions spec/check_codeword_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "check_codeword"

RSpec.describe "check_codeword method" do
it "takes 'horse' and returns 'Correct! Come in.'" do
result = check_codeword("horse")
expect(result).to eq "Correct! Come in."
end

it "takes codeword.chars.first == 'h' && codeword.chars.last == 'e'
and returns 'Close, but nope.'" do
result = check_codeword("home")
expect(result).to eq "Close, but nope."
end

it "takes the incorrect input and returns 'WRONG!'" do
result = check_codeword("chair")
expect(result).to eq "WRONG!"
end
end
11 changes: 11 additions & 0 deletions spec/counter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "counter"

RSpec.describe Counter do
it "add the num to the count variable" do
counter = Counter.new
counter.add(5)
result = counter.report
expect(result).to eq "Counted to 5 so far."
end

end
11 changes: 11 additions & 0 deletions spec/gratitudes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "gratitudes"

RSpec.describe Gratitudes do
it "takes the input gratitude and it pushes to the array. It also formates it" do
gratitudes = Gratitudes.new
gratitudes.add("thank you")
result = gratitudes.format
expect(result).to eq "Be grateful for: thank you"
end

end
8 changes: 8 additions & 0 deletions spec/greet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "greet"

RSpec.describe "greet method" do
it "takes the input and returns Hello, Nagore" do
result = greet("Nagore")
expect(result).to eq "Hello, Nagore!"
end
end
14 changes: 14 additions & 0 deletions spec/password_checker_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "password_checker"

RSpec.describe PasswordChecker do
it "sees the password and returns true if it has 8+ characters." do
password_checker = PasswordChecker.new
result = password_checker.check("carrotss")
expect(result).to eq true
end

it "fails if the password has less that 8 chars" do
password_checker = PasswordChecker.new
expect{ password_checker.check("cat") }.to raise_error "Invalid password, must be 8+ characters."
end
end
21 changes: 21 additions & 0 deletions spec/present_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "present"

RSpec.describe Present do

it "wraps and unwraps a value" do
present = Present.new
present.wrap(10)
expect(present.unwrap).to eq 10
end

it "fails if we unwrap without wrapping first" do
present = Present.new
expect { present.unwrap }.to raise_error "No contents have been wrapped."
end

it "fails if we wrap if we've already wrapped" do
present = Present.new
present.wrap(10)
expect { present.wrap (11) }.to raise_error "A contents has already been wrapped."
end
end
Empty file added spec/reminder_2.rb
Empty file.
13 changes: 13 additions & 0 deletions spec/reminder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'reminder'

# We use the class name here rather than a string
RSpec.describe Reminder do
it "reminds the user to do a task" do
reminder = Reminder.new("Kay")
reminder.remind_me_to("Walk the dog")
result = reminder.remind()
expect(result).to eq "Walk the dog, Kay!"
end

# We would typically have a number of these examples.
end
9 changes: 9 additions & 0 deletions spec/report_length_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "report_length"

RSpec.describe "report_length method" do

it "given string 'hello' returns 'This string was 5 characters long.'" do
result = report_length("hello")
expect(result). to eq "This string was 5 characters long."
end
end
11 changes: 11 additions & 0 deletions spec/string_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "string_builder"

RSpec.describe StringBuilder do
it "takes the input, add it to the string and it counts the string characters" do
string_builder = StringBuilder.new
string_builder.add("Hello")
string_builder.size
result = string_builder.output
expect(result).to eq "Hello"
end
end

0 comments on commit 86602c3

Please sign in to comment.