Dynamic constant assignment Ruby [duplicate] - ruby

This question already has answers here:
Dynamic constant assignment
(7 answers)
Closed 2 years ago.
I have been following a tutorial to create a typing challenge. I have taken care to follow this carefully. When i try to run the script from the command line i keep getting the following error and i do not understand it. I think the tutorial might be quite old but if someone could give me some guidance to understand it so i can fix it then that would be so appreciated! The error i get when i run the script from the command line is as follows....
Typechallenge.rb:89: dynamic constant assignment
Console_Screen = Screen.new
^
typechallenge.rb:90: dynamic constant assignment
Typing_Test = Test.new
The script itself is below...
#Script name: Typing Challenge
#Description: Demonstrating how to apply conditional logic in order to analyze user input and control
#the execution of the script through a computer typing test.
class Screen
def cls
puts ("\n" * 25)
puts "\a"
end
def pause
STDIN.gets
end
end
class Test
def display_greeting
Console_Screen.cls
print "\t\t Welcome to the Typing Challenge" +
"\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +
"continue. \n\n: "
Console_Screen.pause
end
def display_instructions
Console_Screen.cls
puts "\t\t\tInstructions:\n\n"
puts %Q{ This test consists of five typing challenges. Each sentence is a challenge and are presented one at a time. To respond
correctly you should retype each sentence exactly as it is shown and the press the Enter key. Your grade will be displayed at
the end of the test.\n\n\n\n\n\n\n\n\n
Press Enter to continue.\n\n}
Console_Screen.pause
End
def present_test(challenge)
Console_Screen.cls
print challenge + "\n\n: "
result = STDIN.gets
result.chop!
if challenge == result then
$noRight += 1
Console_Screen.cls
print "Correct!\n\nPress Enter to continue."
Console_Screen.pause
else
Console_Screen.cls
print "Incorrect!\n\nPress Enter to continue."
Console_Screen.pause
end
end
def determine_grade
Console_Screen.cls
if $noRight >= 3 then
print "You retyped " + $noRight.to_s + " sentence(s) correctly. "
puts "You have passed the typing test!\n\nPress Enter to continue."
else
print "You retyped " + $noRight.to_s + " sentence(s) correctly. "
puts "You have failed the typing test!\n\nPress Enter to continue."
end
end
#Main script logic
$noRight = 0
Console_Screen = Screen.new
Typing_Test = Test.new
Typing_Test.display_greeting
Console_Screen.cls
print "Would you like to test your typing skills? (y/n)\n\n: "
answer = STDIN.gets
answer.chop!
until answer == "y" || answer == "n"
Console_Screen.cls
print "Would you like to test your typing skills? (y/n)\n\n: "
answer = STDIN.gets
answer.chop!
end
#Analyzing the players response
if answer == "n"
Console_Screen.cls
puts "Okay, perhaps another time! \n\n"
else
Typing_Test.display_instructions
Typing_Test.present_test "In the end there can be only one"
Typing_Test.present_test "Once upon a time a great plague swept across the land"
Typing_Test.present_test "Welcome to the typing challenge"
Typing_Test.present_test "There are very few problems in the world" + "that enough M&Ms cannot fix."
Typing_Test.present_test "Lets play this game of life together"
Typing_Test.determine_grade
Console_Screen.pause
Console_Screen.cls
puts "Thank you for playing the game!\n\n"
end
end
end

Names that start with upper-case letter are constants. In your code you assign a non-constant (dynamic) value to a name that represents a constant. Hence the error.
Console_Screen = Screen.new
Use local variable name convention (snake_case)
console_screen = Screen.new

Related

How do I display the largest number in an array in Ruby? [duplicate]

This question already has answers here:
How to find a min/max with Ruby
(5 answers)
Closed 4 years ago.
I have a homework assignment that I need to finish. I think most of the code is working but I am having trouble with the last part. I need to display the largest number that a user enters (into an array). Below is the code I have so far. I am open to any suggestions. Thanks in advance.
Here's the description of the assignment:
Write a Ruby application that allows a user to input a series of 10 integers and determines and prints the largest integer. Your program should use at least the following three variables:
a) counter: A counter to count to 10 (i.e., to keep track of how many numbers have been input and to determine when all 10 numbers have been processed).
b) number: The integer most recently input by the user.
c) largest: The largest number found so far.
class Screen
def cls
puts ("\n")
puts "\a"
end
def pause
STDIN.gets
end
end
class Script
def display_instructions
Console_Screen.cls
print "This script will take the user input of 10 integers and then
will
print the largest."
print "\n\nPress enter to continue."
Console_Screen.cls
Console_Screen.pause
end
def getNumber #accepts user input
list = Array.new
10.times do
Console_Screen.cls
print "This script accepts 10 integers."
print "\n\nPlease type an integer and press enter."
input = STDIN.gets
input.chop!
list.push(input)
end
end
def display_largest(number) #displays the largest integer entered by the
user
Console_Screen.cls
print "The largest integer is " +
end
def runScript
number = getNumber
Console_Screen.cls
display_largest(number)
end
end
#Main Script Logic
Console_Screen = Screen.new
LargestNum = Script.new
answer = ""
loop do
Console_Screen.cls
print "Are you ready to start the script? (y/n): "
print "\n\nWould you like instructions on how this script works? (h): "
answer = STDIN.gets
answer.chop!
break if answer =~ /y|n|h/i
end
if answer == "h" or answer == "H"
LargestNum.display_instructions
print "Are you ready to start the script? (y/n): "
answer = STDIN.gets
answer.chop!
end
if answer == "n" or answer == "N"
Console_Screen.cls
puts "Okay, maybe another time.\n\n"
Console_Screen.pause
else
loop do
LargestNum.runScript
print "\n\nEnter Q to quit or press any key to run the script again: "
runAgain = STDIN.gets
runAgain.chop!
break if runAgain =~ /Q/i
end
end
This question has been asked and answered so many times before. Personally I think, as this answer suggests, the built in .max is the best solution.
[1, 3, 5].max #=> 5
Have you learned about for loops yet? You have to iterate through the array. For a very trivial example, you can do something like
max = 0
for element in list
if element > max
max= element
return max

Ruby script crashes [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have the following code that I am trying to learn from this Ruby book and it keeps crashing, I have spent hours trying to fix. If anyone has any clue why this is happening please let me know. I am not enjoying Ruby
# Define custom classes ---------------------------------------------------
#Define a class representing the console window
class Screen
def cls #Define a method that clears the display area
puts ("\n" * 25) #Scroll the screen 25 times
puts "\a" #Make a little noise to get the player's attention
end
def pause #Define a method that pauses the display area
STDIN.gets #Execute the STDIN class's gets method to pause script
#execution until the player presses the enter key
end
end
#Define a class representing the Ruby Number Guessing Game
class Game
#This method displays the game's opening screen
def display_greeting
Console_Screen.cls #Clear the display area
#Display welcome message
print "\t\t Welcome to the Ruby Number Guessing Game!" +
"\n\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +
"continue."
Console_Screen.pause #Pause the game
end
#Define a method to be used to present game instructions
def display_instructions
Console_Screen.cls #Clear the display area
puts "INSTRUCTIONS:\n\n" #Display a heading
#Display the game's instructions
puts "This game randomly generates a number from 1 to 100 and"
puts "challenges you to identify it in as few guesses as possible."
puts "After each guess, the game will analyze your input and provide"
puts "you with feedback. You may take as many turns as you need in"
puts "order to guess the game's secret number.\n\n"
puts "Game will stop if you have guessed 10 times.\n\n\n"
puts "Good luck!\n\n\n\n\n\n\n\n\n"
print "Press Enter to continue."
Console_Screen.pause #Pause the game
end
#Define a method that generates the game's secret number
def generate_number
#Generate and return a random number between 1 and 100
return randomNo = 1 + rand(1000)
end
#Define a method to be used control game play
def play_game
#Call on the generate_number method in order to get a random number
number = generate_number
#Loop until the player inputs a valid answer
loop do
Console_Screen.cls #Clear the display area
if answer == "c"
print "Game count : #{$gameCount}"
end
#Prompt the player to make a guess
print "\nEnter your guess and press the Enter key: "
reply = STDIN.gets #Collect the player's answer
reply.chop! #Remove the end of line character
reply = reply.to_i #Convert the player's guess to an integer
#Validate the player's input only allowing guesses between 1 and 100
if reply < 1 or reply > 1000 then
redo #Redo the current iteration of the loop
end
#Analyze the player's guess to determine if it is correct
if reply == number then #The player's guess was correct
Console_Screen.cls #Clear the display area
print "You have guessed the number! Press enter to continue."
Console_Screen.pause #Pause the game
break #Exit loop
elsif reply < number then #The player's guess was too low
Console_Screen.cls #Clear the display area
print "Your guess is too low! ( valid range: 1 - 1000) Press Enter to continue."
Console_Screen.pause #Pause the game
elsif reply > number then #The player's guess was too high
Console_Screen.cls #Clear the display area
print "Your guess is too high! ( valid range: 1 - 1000) Press Enter to continue."
Console_Screen.pause #Pause the game
end
$noOfGuesses +=1
break if $noOfGuesses > 10
end
end
#This method displays the information about the Ruby Number Guessing Game
def display_credits
Console_Screen.cls #Clear the display area
#Thank the player and display game information
puts "\t\tThank you playing the Ruby Number Guessing Game.\n\n\n\n"
puts "\n\t\t\t Developed by Jerry Lee Ford, Jr.\n\n"
puts "\t\t\t\t Copyright 2010\n\n"
puts "\t\t\tURL: http://www.tech-publishing.com\n\n\n\n\n\n\n\n\n\n"
end
end
# Main Script Logic -------------------------------------------------------
Console_Screen = Screen.new #Instantiate a new Screen object
SQ = Game.new #Instantiate a new Game object
#Execute the Game class's display_greeting method
SQ.display_greeting
answer = ""
$gameCount = 0
$noOfGuesses = 0
$totalNoOfGuesses = 0
$avgNoOfGuesses = 0
#Loop until the player enters y or n and do not accept any other input
loop do
Console_Screen.cls #Clear the display area
#Prompt the player for permission to start the game
print "Are you ready to play the Ruby Number Guessing Game? (y/n): "
answer = STDIN.gets #Collect the player's response
answer.chop! #Remove any extra characters appended to the string
#Terminate the loop if valid input was provided
break if answer == "y" || answer == "n" || answer == "c" #Exit loop
end
#Analyze the player's input
if answer == "n" #See if the player elected not to take the game
Console_Screen.cls #Clear the display area
#Invite the player to return and play the game some other time
puts "Okay, perhaps another time.\n\n"
else #The player wants to play the game
#Execute the Game class's display_instructions method
SQ.display_instructions
loop do
$gameCount+=1
#Execute the Game class's play_game method
SQ.play_game
$totalNoOfGuesses = $noOfGuesses * $gameCount
$avgNoOfGuesses = $totalNoOfGuesses / $noOfGuesses
print "The total number of guesses was #{$totalNoOfGuesses}"
print "The average number of guesses was #{$avgNoOfGuesses}"
Console_Screen.pause #Pause the game
print "Press Enter to continue"
Console_Screen.cls #Clear the display area
#Prompt the player for permission start a new round of play
print "Would you like to play again? (y/n): "
playAgain = STDIN.gets #Collect the player's response
playAgain.chop! #Remove any extra characters appended to the string
break if playAgain == "n" #Exit loop
end
#Call upon the Game class's determine_credits method in order to thank
#the player for playing the game and to display game information
SQ.display_credits
end
When running the code it says:
script-not-working.rb:74:in `block in play_game': undefined local variable or method `answer' for #<Game:0x0000000180f9c0> (NameError)
from script-not-working.rb:70:in `loop'
from script-not-working.rb:70:in `play_game'
from script-not-working.rb:181:in `block in <main>'
from script-not-working.rb:176:in `loop'
from script-not-working.rb:176:in `<main>'
So one solution could be make the variable answer global, adding $ before all answer variables it should look like : $answer. The code use other global variables so it could be fine for this. There are better practices than these but for this code it works fine. After that the game is running correctly. But it seems that has some other problems for evaluating the number. this should be another fix. maybe for another question. So investigate thought your code.
Here is the result of the code making answer global using $answer:
#Define a class representing the console window
class Screen
def cls #Define a method that clears the display area
puts ("\n" * 25) #Scroll the screen 25 times
puts "\a" #Make a little noise to get the player's attention
end
def pause #Define a method that pauses the display area
STDIN.gets #Execute the STDIN class's gets method to pause script
#execution until the player presses the enter key
end
end
#Define a class representing the Ruby Number Guessing Game
class Game
#This method displays the game's opening screen
def display_greeting
Console_Screen.cls #Clear the display area
#Display welcome message
print "\t\t Welcome to the Ruby Number Guessing Game!" +
"\n\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +
"continue."
Console_Screen.pause #Pause the game
end
#Define a method to be used to present game instructions
def display_instructions
Console_Screen.cls #Clear the display area
puts "INSTRUCTIONS:\n\n" #Display a heading
#Display the game's instructions
puts "This game randomly generates a number from 1 to 100 and"
puts "challenges you to identify it in as few guesses as possible."
puts "After each guess, the game will analyze your input and provide"
puts "you with feedback. You may take as many turns as you need in"
puts "order to guess the game's secret number.\n\n"
puts "Game will stop if you have guessed 10 times.\n\n\n"
puts "Good luck!\n\n\n\n\n\n\n\n\n"
print "Press Enter to continue."
Console_Screen.pause #Pause the game
end
#Define a method that generates the game's secret number
def generate_number
#Generate and return a random number between 1 and 100
return randomNo = 1 + rand(1000)
end
#Define a method to be used control game play
def play_game
#Call on the generate_number method in order to get a random number
number = generate_number
#Loop until the player inputs a valid answer
loop do
Console_Screen.cls #Clear the display area
if $answer == "c"
print "Game count : #{$gameCount}"
end
#Prompt the player to make a guess
print "\nEnter your guess and press the Enter key: "
reply = STDIN.gets #Collect the player's answer
reply.chop! #Remove the end of line character
reply = reply.to_i #Convert the player's guess to an integer
#Validate the player's input only allowing guesses between 1 and 100
if reply < 1 or reply > 1000 then
redo #Redo the current iteration of the loop
end
#Analyze the player's guess to determine if it is correct
if reply == number then #The player's guess was correct
Console_Screen.cls #Clear the display area
print "You have guessed the number! Press enter to continue."
Console_Screen.pause #Pause the game
break #Exit loop
elsif reply < number then #The player's guess was too low
Console_Screen.cls #Clear the display area
print "Your guess is too low! ( valid range: 1 - 1000) Press Enter to continue."
Console_Screen.pause #Pause the game
elsif reply > number then #The player's guess was too high
Console_Screen.cls #Clear the display area
print "Your guess is too high! ( valid range: 1 - 1000) Press Enter to continue."
Console_Screen.pause #Pause the game
end
$noOfGuesses +=1
break if $noOfGuesses > 10
end
end
#This method displays the information about the Ruby Number Guessing Game
def display_credits
Console_Screen.cls #Clear the display area
#Thank the player and display game information
puts "\t\tThank you playing the Ruby Number Guessing Game.\n\n\n\n"
puts "\n\t\t\t Developed by Jerry Lee Ford, Jr.\n\n"
puts "\t\t\t\t Copyright 2010\n\n"
puts "\t\t\tURL: http://www.tech-publishing.com\n\n\n\n\n\n\n\n\n\n"
end
end
# Main Script Logic -------------------------------------------------------
Console_Screen = Screen.new #Instantiate a new Screen object
SQ = Game.new #Instantiate a new Game object
#Execute the Game class's display_greeting method
SQ.display_greeting
$answer = ""
$gameCount = 0
$noOfGuesses = 0
$totalNoOfGuesses = 0
$avgNoOfGuesses = 0
#Loop until the player enters y or n and do not accept any other input
loop do
Console_Screen.cls #Clear the display area
#Prompt the player for permission to start the game
print "Are you ready to play the Ruby Number Guessing Game? (y/n): "
$answer = STDIN.gets #Collect the player's response
$answer.chop! #Remove any extra characters appended to the string
#Terminate the loop if valid input was provided
break if $answer == "y" || $answer == "n" || $answer == "c" #Exit loop
end
#Analyze the player's input
if $answer == "n" #See if the player elected not to take the game
Console_Screen.cls #Clear the display area
#Invite the player to return and play the game some other time
puts "Okay, perhaps another time.\n\n"
else #The player wants to play the game
#Execute the Game class's display_instructions method
SQ.display_instructions
loop do
$gameCount+=1
#Execute the Game class's play_game method
SQ.play_game
$totalNoOfGuesses = $noOfGuesses * $gameCount
$avgNoOfGuesses = $totalNoOfGuesses / $noOfGuesses
print "The total number of guesses was #{$totalNoOfGuesses}"
print "The average number of guesses was #{$avgNoOfGuesses}"
Console_Screen.pause #Pause the game
print "Press Enter to continue"
Console_Screen.cls #Clear the display area
#Prompt the player for permission start a new round of play
print "Would you like to play again? (y/n): "
playAgain = STDIN.gets #Collect the player's response
playAgain.chop! #Remove any extra characters appended to the string
break if playAgain == "n" #Exit loop
end
#Call upon the Game class's determine_credits method in order to thank
#the player for playing the game and to display game information
SQ.display_credits
end
The problem is in your play_game method within the Game class, there is no where the variable answer is defined i.e assigned to a value, even an empty value. So i edited your script such that the method takes answer as an arguement, when the the method is called later here, you pass the answer expected from the console as its argument.
SQ.play_game answer
Here is the edited script below
#Define a class representing the console window
class Screen
def cls #Define a method that clears the display area
puts ("\n" * 25) #Scroll the screen 25 times
puts "\a" #Make a little noise to get the player's attention
end
def pause #Define a method that pauses the display area
STDIN.gets #Execute the STDIN class's gets method to pause script
#execution until the player presses the enter key
end
end
#Define a class representing the Ruby Number Guessing Game
class Game
#This method displays the game's opening screen
def display_greeting
Console_Screen.cls #Clear the display area
#Display welcome message
print "\t\t Welcome to the Ruby Number Guessing Game!" +
"\n\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +
"continue."
Console_Screen.pause #Pause the game
end
#Define a method to be used to present game instructions
def display_instructions
Console_Screen.cls #Clear the display area
puts "INSTRUCTIONS:\n\n" #Display a heading
#Display the game's instructions
puts "This game randomly generates a number from 1 to 100 and"
puts "challenges you to identify it in as few guesses as possible."
puts "After each guess, the game will analyze your input and provide"
puts "you with feedback. You may take as many turns as you need in"
puts "order to guess the game's secret number.\n\n"
puts "Game will stop if you have guessed 10 times.\n\n\n"
puts "Good luck!\n\n\n\n\n\n\n\n\n"
print "Press Enter to continue."
Console_Screen.pause #Pause the game
end
#Define a method that generates the game's secret number
def generate_number
#Generate and return a random number between 1 and 100
return randomNo = 1 + rand(1000)
end
#Define a method to be used control game play
def play_game answer
#Call on the generate_number method in order to get a random number
number = generate_number
#Loop until the player inputs a valid answer
loop do
Console_Screen.cls #Clear the display area
if answer == "c"
print "Game count : #{$gameCount}"
end
#Prompt the player to make a guess
print "\nEnter your guess and press the Enter key: "
reply = STDIN.gets #Collect the player's answer
reply.chop! #Remove the end of line character
reply = reply.to_i #Convert the player's guess to an integer
#Validate the player's input only allowing guesses between 1 and 100
if reply < 1 or reply > 1000 then
redo #Redo the current iteration of the loop
end
#Analyze the player's guess to determine if it is correct
if reply == number then #The player's guess was correct
Console_Screen.cls #Clear the display area
print "You have guessed the number! Press enter to continue."
Console_Screen.pause #Pause the game
break #Exit loop
elsif reply < number then #The player's guess was too low
Console_Screen.cls #Clear the display area
print "Your guess is too low! ( valid range: 1 - 1000) Press Enter to continue."
Console_Screen.pause #Pause the game
elsif reply > number then #The player's guess was too high
Console_Screen.cls #Clear the display area
print "Your guess is too high! ( valid range: 1 - 1000) Press Enter to continue."
Console_Screen.pause #Pause the game
end
$noOfGuesses +=1
break if $noOfGuesses > 10
end
end
#This method displays the information about the Ruby Number Guessing Game
def display_credits
Console_Screen.cls #Clear the display area
#Thank the player and display game information
puts "\t\tThank you playing the Ruby Number Guessing Game.\n\n\n\n"
puts "\n\t\t\t Developed by Jerry Lee Ford, Jr.\n\n"
puts "\t\t\t\t Copyright 2010\n\n"
puts "\t\t\tURL: http://www.tech-publishing.com\n\n\n\n\n\n\n\n\n\n"
end
end
# Main Script Logic -------------------------------------------------------
Console_Screen = Screen.new #Instantiate a new Screen object
SQ = Game.new #Instantiate a new Game object
#Execute the Game class's display_greeting method
SQ.display_greeting
answer = ""
$gameCount = 0
$noOfGuesses = 0
$totalNoOfGuesses = 0
$avgNoOfGuesses = 0
#Loop until the player enters y or n and do not accept any other input
loop do
Console_Screen.cls #Clear the display area
#Prompt the player for permission to start the game
print "Are you ready to play the Ruby Number Guessing Game? (y/n): "
answer = STDIN.gets #Collect the player's response
answer.chop! #Remove any extra characters appended to the string
#Terminate the loop if valid input was provided
break if answer == "y" || answer == "n" || answer == "c" #Exit loop
end
#Analyze the player's input
if answer == "n" #See if the player elected not to take the game
Console_Screen.cls #Clear the display area
#Invite the player to return and play the game some other time
puts "Okay, perhaps another time.\n\n"
else #The player wants to play the game
#Execute the Game class's display_instructions method
SQ.display_instructions
loop do
$gameCount+=1
#Execute the Game class's play_game method
SQ.play_game answer
$totalNoOfGuesses = $noOfGuesses * $gameCount
$avgNoOfGuesses = $totalNoOfGuesses / $noOfGuesses
print "The total number of guesses was #{$totalNoOfGuesses}"
print "The average number of guesses was #{$avgNoOfGuesses}"
Console_Screen.pause #Pause the game
print "Press Enter to continue"
Console_Screen.cls #Clear the display area
#Prompt the player for permission start a new round of play
print "Would you like to play again? (y/n): "
playAgain = STDIN.gets #Collect the player's response
playAgain.chop! #Remove any extra characters appended to the string
break if playAgain == "n" #Exit loop
end
#Call upon the Game class's determine_credits method in order to thank
#the player for playing the game and to display game information
SQ.display_credits
end

Ruby: syntax error, unexpected keyword_when, expecting end-of-input

I'm trying to modify a script to include a case block but whenever I do I seem to get this error. I initially thought it was because I missed an end somewhere but I checked my entire code and it seems to check out fine.
Working (before adding case):
def determine_grade
Console_Screen.cls #Clear the display area
#To pass the test the player must correctly retype 3 sentences
if $noRight >= 6 then
#Inform the player of the good news
print "You retyped " + $noRight.to_s + " sentence(s)" +
"correctly. "
puts "You have passed the typing test!\n\nPress Enter" +
"to continue."
else #The player has failed the test
#Inform the player of the bad news
print "You retyped " + $noRight.to_s + " sentence(s)" +
"correctly. "
puts "You have failed the typing test!\n\nPress Enter
to continue."
end
end
end
After:
def determine_grade
Console_Screen.cls #Clear the display area
#To pass the test the player must correctly retype 6 sentences
case $noRight
when 9 || 10
print "You get an A!"
end
when 8
print "You get a B!"
end
when 7
print "You get a C."
end
when 6
print "You get a D."
end
when <= 5
print "You get an F."
end
else
print "Error"
end
end
end
end
Any ideas?
You don't need the end statements inside of the when block. That's what's throwing the interpreter off. Also, your multi-value when is wrong; it should be when 9, 10 instead of when 9 || 10 since that will evaluate to a truthy value.
Remove those and your new code should be equivalent to your original code.
def determine_grade
Console_Screen.cls #Clear the display area
#To pass the test the player must correctly retype 6 sentences
print case $noRight
when 9..10 then "You get an A!"
when 8 then "You get a B!"
when 7 then "You get a C."
when 6 then "You get a D."
when -Float::INFINITY..5 then "You get an F."
else "Error"
end
end

Ruby script need fix

I'm having a problem with my ruby script. If anyone could help, I'd really appreciate it. The problem is that the number is stuck between 1-2; where 2 is too high and 1 is too low. The guesses should be integers only.
#!/usr/bin/ruby
def highLow(max)
again = "yes"
while again == "yes"
puts "Welcome to the High Low game"
playGame(max)
print "Would you like to play again? (yes/no): "
again = STDIN.gets.chomp
if again == 'no'
puts "Have a nice day, Goodbye"
end
end
end
#This method contains the logic for a single game and call the feedback method.
def playGame(max)
puts "The game gets played now"
puts "I am thinking of a number between 1 and #{max}." #It show what chosen by user
randomNumber = rand(max)+ 1
print "Make your guess: "
guess = STDIN.gets.chomp
feedback(guess, randomNumber)
end
#Start while loop
#Logic for feedback method. It's ganna check the guess if it's high or low.
def feedback(guess, randomNumber)
count = 1
while guess.to_i != randomNumber
count = count + 1
if guess.to_i < randomNumber
print "That's too low. Guess again: "
else
print "That's too high. Guess again: "
end
guess = STDIN.gets.chomp
end
puts "Correct! You guessed the answer in #{count} tries!"
end
highLow(ARGV[0])
Change your last line to this:
highLow(ARGV[0].to_i)
The ARGV array contains all the passed in arguments as strings, so you have to cast it to integer.

I need help in ruby

I'm making a software but I don't want to publish it's source code right now because I don't want people to steal my hard work. I'm not rude or anything like that. Below is an example of what the program I'm making looks like.
print "Username : "
name = gets.chomp
print "Password : "
pass = gets.chomp
if name =="user" and pass=="pa$$word"
print "Hello"
else print "Error, incorrect details"
Now this is a simplest login form in ruby but what bad happens here is whenever the user inserts wrong information the program will simply shutdown and what I want to happen is that I want to keep the program asking the user for right information until right information is inserted.
Are you a windows user? Do know how to program in batch files?
examples
echo Hello world
cls
pause
So here is the code for ruby
a. print "Command : "
b. command = gets.chomp
c. if command == "Hello"
d. print "Hi, how are you?"
e. elsif command == "help"
f. print "Say hi to me, chat with me"
now what I want here too is just like in the first question
Details : After the user types in "Hi" the program just shuts down but what I want here it to make the program ask go to line a again
Use a while loop which continually asks for entry and checks the user's submission until a valid result is entered.
username = nil
while username.nil?
puts "What is your username?"
entered_username = gets.chomp
if entered_username == "Bob"
username = entered_username
end
end
puts "Thanks!"
When run on the terminal this produces:
What is your username?
sdf
What is your username?
dsfsd
What is your username?
sdfsd
What is your username?
sdfds
What is your username?
sdf
What is your username?
Bob
Thanks!
1.
until (print "Username : "; gets.chomp == "user") and
(print "Password : "; gets.chomp == "pa$$word")
puts "Error, incorrect details"
end
puts "Hello"
2.
loop do
print "Command : "
case gets.chomp
when "Hello" then print "Hi, how are you?"; break
when "help" then print "Say hi to me, chat with me"; break
end
end
Here is the easy method :) If anyone is stuck with the problem I encountered all you do is this. The "while" loop
number = 1 # Value we will give for a string named "number"
while number < 10 # The program will create a loop until the value
# of the string named "number" will be greater than 10
puts "hello"
# So what we need to do now is to end the loop otherwise
# it will continue on forever
# So how do we do it?
# We will make the ruby run a script that will increase
# the string named "number"'s value every time we run the loop
number = number + 1
end # Now what happens is every time we run the code all the program
# will do is print "hello" and add number 1 to the string "number".
# It will continue to print out "hello" until the string is
# greater than 10

Resources