Need help with Ruby-related code; I'm tackling sample practice problems and in the selected areas, I wanted to know how I can properly define and adjust the code to function. The ", :pending => true" parts have been removed/scratched out in order to allow the code to check if it works. (It's for checking purposes, ignore it)  Topic: Case-sensitive and If else statement checks

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Need help with Ruby-related code; I'm tackling sample practice problems and in the selected areas, I wanted to know how I can properly define and adjust the code to function. The ", :pending => true" parts have been removed/scratched out in order to allow the code to check if it works. (It's for checking purposes, ignore it) 

Topic: Case-sensitive and If else statement checks

l- class HangpersonGame
2
# add the necessary class methods, attributes, etc. here
# to make the tests in spec/hangperson_game_spec.rb pass.
3
# Get a word from remote "random word" service
7
8
# def initialize()
9
# end
10
11 -
def initialize (word)
12
@word = word
13
@guesses = ""
14
@wrong_guesses = ""
15 ,
end
16
17
def word
18
return @word
19
end
20
21
def guesses
22
return @guesses
23
end
24
25 ,
def wrong_guesses
26
return @wrong_guesses
27 -
end
28
def guess (letter)
if @word.include? (letter)
@guesses += letter
29 ,
30 -
31 -
32
return true
33
else
@wrong_guesses += letter
end
34
35
36
end
37
def word_with_guesses
partial_matches = ""
@word.each_char do |w|
partial_matches += "-"
end
38 -
39
40 -
41
42
43
return partial_matches
44
end
45
# You can test it by running $ bundle exec irb -I. -r app.rb
# And then in the irb: irb(main):001:0> HangpersonGame.get_random_word
=> "cooking"
def self.get_random_word
require 'uri'
require 'net/http'
uri = URI('http://randomword.saasbook.info/RandomWord')
Net::HTTP.new ( 'randomword.saasbook.info').start { |http|
return http. post (uri, "").body
}
46
47
48
#3
<-- some random word
49 ,
50
51
52
53 ,
54
55
56
end
57
58
end
59
Transcribed Image Text:l- class HangpersonGame 2 # add the necessary class methods, attributes, etc. here # to make the tests in spec/hangperson_game_spec.rb pass. 3 # Get a word from remote "random word" service 7 8 # def initialize() 9 # end 10 11 - def initialize (word) 12 @word = word 13 @guesses = "" 14 @wrong_guesses = "" 15 , end 16 17 def word 18 return @word 19 end 20 21 def guesses 22 return @guesses 23 end 24 25 , def wrong_guesses 26 return @wrong_guesses 27 - end 28 def guess (letter) if @word.include? (letter) @guesses += letter 29 , 30 - 31 - 32 return true 33 else @wrong_guesses += letter end 34 35 36 end 37 def word_with_guesses partial_matches = "" @word.each_char do |w| partial_matches += "-" end 38 - 39 40 - 41 42 43 return partial_matches 44 end 45 # You can test it by running $ bundle exec irb -I. -r app.rb # And then in the irb: irb(main):001:0> HangpersonGame.get_random_word => "cooking" def self.get_random_word require 'uri' require 'net/http' uri = URI('http://randomword.saasbook.info/RandomWord') Net::HTTP.new ( 'randomword.saasbook.info').start { |http| return http. post (uri, "").body } 46 47 48 #3 <-- some random word 49 , 50 51 52 53 , 54 55 56 end 57 58 end 59
context 'invalid' do
before :each do
@game = HangpersonGame.new ('foobar')
end
it 'throws an error when empty', 'pending
expect { @game.guess ('') }.to raise_error (ArgumentError)
end
true de
it 'throws an error when not a letter' nending - true do
expect { @game.guess ('%') }. to raise_error (ArgumentError)
end
it 'throws an error when nil', ponding= truo do
expect { @game.guess (nil) }.to raise_error (ArgumentError)
end
end
end
Transcribed Image Text:context 'invalid' do before :each do @game = HangpersonGame.new ('foobar') end it 'throws an error when empty', 'pending expect { @game.guess ('') }.to raise_error (ArgumentError) end true de it 'throws an error when not a letter' nending - true do expect { @game.guess ('%') }. to raise_error (ArgumentError) end it 'throws an error when nil', ponding= truo do expect { @game.guess (nil) }.to raise_error (ArgumentError) end end end
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education