Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Just started to go through the 7 languages in 7 weeks book; day one problem make a simple higher or lower game in ruby. When I execute my script I get the following error but I don't know why.
EDIT: This is thrown after I guess once.
NameError: undefined local variable or method actual' for main:Object
from guess_game.rb:2:inguess'
from guess_game.rb:22
def guess(guess, acutal)
unless guess == actual
if guess > actual
puts 'Lower'
else
puts 'Higher'
return false
end
end
puts 'Correct'
return true
end
answer = rand(10)
game_won = false
puts 'I am thinking of a number, what is it?'
until game_won
num = gets.to_i
# Static Debug Line
puts "Guess #{num} : Answer #{answer}"
game_won = guess(num, answer)
end
def guess(guess, actual)
unless guess == actual
if guess > actual
puts 'Lower'
else
puts 'Higher'
return false
end
end
puts 'Correct'
return true
end
Problem: Spelling mistake, Corrected
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am a total beginner to Ruby. I am trying to print if an object is an array or an integer, but I am getting a syntax error I cannot figure how to solve it.
D:\Ruby>ruby -c Learning-Ruby\loops_stuff.rb
Learning-Ruby/loops_stuff.rb:9: syntax error, unexpected keyword_else, expecting keyword_end
Learning-Ruby/loops_stuff.rb:11: syntax error, unexpected end-of-input, expecting keyword_end
This is my code
obj = ["a", 1, 3.6]
if object.is_a(obj)
puts "Is array: "
obj.each do |index|
puts index
elseif object.is_i(obj)
puts "Is integer: {#obj}"
else
puts "Is neither array or integer"
end
the keyword is elsif (without the e in the middle)
obj = ["a", 1, 3.6]
if obj.is_a?(Array)
puts "Is array: "
obj.each do |index|
puts index
end
elsif obj.is_a?(Integer)
puts "Is integer: #{obj}"
else
puts "Is neither array or integer"
end
also stumbled over this in my first ruby sessions
For addition, you can use case..when statement (looks more elegant as for me):
case obj
when Integer
#some actions
when Array
#some actions
else
#some actions
end
According to the strings you print, this might be what you want to code.
obj = ["a", 1, 3.6]
if obj.is_a?(Array)
puts "Is array: "
obj.each do |index|
puts index
end
elsif obj.is_a?(Integer)
puts "Is integer: {#obj}"
else
puts "Is neither array or integer"
end
The output is:
Is array:
a
1
3.6
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'm trying to define a method that countdowns 10 to 0 and at the end returns HAPPY NEW YEARS! however i don't want i"am doing wrong?
def countdown(number)
while number > 0
puts "#{number} SECONDS(S)!"
number -= 1
end
"HAPPY NEW YEAR!"
end
A quick Google search revealed that you are apparently trying to solve https://learn.co/lessons/countdown-to-midnight (you really should have included that link)
Your code is not passing the spec, because it contains an additional S:
puts "#{number} SECONDS(S)!"
# ^
It has to be:
puts "#{number} SECOND(S)!"
def countdown(number)
while number > 0
puts "#{number} SECONDS(S)!"
number -= 1
end
puts "HAPPY NEW YEAR!"
end
I added a puts on the last line of your code. You method is counting down to 0 seconds, but the last line only return the string "HAPPY NEW YEAR!", and does not print it to the screen. Or if you need to return the string and print it on the screen, you can do p "HAPPY NEW YEAR!"
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 7 years ago.
Improve this question
I have two methods turn_count(board) and current_player(board). The turn_count(board) method, which returns the number of "X"s and "O"s in an array is working appropriately. But the current_player(board) method, which is supposed to puts "X" if turn_count(board) is even and "O" if turn_count(board) is odd keeps giving me an error. The error is:
/Users/john/Desktop/test file.rb:13:in current_player': undefined method%' for nil:NilClass (NoMethodError)
from /Users/john/Desktop/test file.rb:18:in `'
Clearly it's saying there's an issue with the modulo operator being used, but i'm not sure why and have been wracking my brain trying to figure it out.
Any help is greatly appreciated!
def turn_count(board)
count = 0
board.each do |x| if x == "X" || x == "O"
count = count + 1
end
end
puts count
end
def current_player(board)
if turn_count(board) % == 0
puts "X"
else
puts "O"
end
end
The problem is you are using % on a NilClass. Your turn_count() method returns nil. Check what happens if you replace puts count with count.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I get an error while trying to program ruby.
Error : C:/Users/PC ASUS/Desktop/g.rb:3:in <main>': undefined local variable or method 'y' for main:Object (NameError)
Here is my code:
puts " Do you like to install hacking pack?"
insta_one = gets.chomp
if insta_one == y
make
else
puts "Ok. Bye!"
end
def make
awe = file.new("shell.bat","w")
readme.puts("#echo off")
readme.puts("color a")
readme.puts("echo Installing hacking pack")
readme.puts("Thanks for downloading rootShell!")
readme.puts("My email - cyniclimbu#gmail.com")
end
y is not string but undefined variable.
Please change line 3:
if insta_one == 'y'
Before you can use a method, you need to define it. In your code, make is defined after you try to call it, so ruby is not familiar with it, and throws an error:
def make
File.open("shell.bat", 'w') do |readme|
readme.puts("#echo off")
readme.puts("color a")
readme.puts("echo Installing hacking pack")
readme.puts("Thanks for downloading rootShell!")
readme.puts("My email - cyniclimbu#gmail.com")
end
end
puts " Do you like to install hacking pack?"
insta_one = gets.chomp
if insta_one == 'y'
make
else
puts "Ok. Bye!"
end
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am following a beginners course on Ruby, trying to define two methods in the editor:
A greeter method that takes a single string parameter, name, and returns a string greeting that person. (Make sure to use return and don't use print or puts.)
A by_three? method that takes a single integer parameter, number, and returns true if that number is evenly divisible by three and false if not.
The error I'm getting is "unexpected end".
def greeter(name)
return "hey" + name + "how are you" + "."
end
greeter(alan)
def by_three?(number)
if number % 3 == 0
return true
else
return false
end
by_three?(12)
You should terminate if statement with end keyword:
def by_three?(number)
if number % 3 == 0
return true
else
return false
end
end
Having said that, this method is written really bad, and it can be much simpler:
def by_three?(number)
number % 3 == 0
end