Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions lib/rubies/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ def itsright
puts "Correct!".colorize(:green)
end

def cheater
@num_wrong += 1
puts "Please provide an answer "\
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not write to stdout. Use Rails' logger if you want to log.

"that operates on the given values. ".colorize(:light_red)
puts
puts "Try not to directly reference the value directly, ".colorize(:light_red)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [84/80]
Do not write to stdout. Use Rails' logger if you want to log.

puts
puts "Instead, try accessing the value by its index or key!".colorize(:light_red)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [87/80]
Do not write to stdout. Use Rails' logger if you want to log.

end

def prompter(answer)
print "Write ruby code to find the following value".colorize(:light_blue)
print " (or enter ".colorize(:light_blue) + 'NEW'.colorize(:green)
Expand Down Expand Up @@ -155,6 +165,45 @@ def check_answer(current, input, target)
end
end

# deletes the target from the current data structure
def deep_delete(current, target)
return current unless current.is_a? Enumerable
current.
reject {|value| value == target}.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.
Use 2 (not 4) spaces for indenting an expression spanning multiple lines.
Space between { and | missing.
Space missing inside }.

map {|value| deep_delete(value, target)}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.
Use 2 (not 4) spaces for indenting an expression spanning multiple lines.
Space between { and | missing.
Space missing inside }.

end

# creates an array of values for which any given answer should fail
def tests(current, target)
[ nil,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside square brackets detected.

Array.new,
Hash.new,
RandomDataStructure.new.generate,
deep_delete(current, target)
]
end

# test user input against various tests, for each of which the
# input should fail. Returns true only if the user input fails
# each of the tests.
def test_answer(current, input, target)
tests(current, target).all? do |test|
!run_test(test, input, target)
end
end

# test whether the answer provided by the given
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

# routine matches the target answer
def run_test(current, input, target)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused method argument - current. If it's necessary, use _ or _current as an argument name to indicate that it won't be used.

routine = lambda { eval(input) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of eval is a serious security risk.

begin
output = routine.call
output == target
rescue Exception
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid rescuing the Exception class. Perhaps you meant to rescue StandardError?

false
end
end

def generate_data_structure
rds = RandomDataStructure.new
current = rds.generate
Expand All @@ -175,8 +224,12 @@ def play_round # new, exit or check if right/wrong
return
else
if check_answer(current, input, target)
itsright
correct = true
if !test_answer(current, input, target)
cheater
else
itsright
correct = true
end
else
itswrong(target)
end
Expand Down