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 2 years ago.
Improve this question
I want to do the sums of multiples of 3 or 5 untill 1000 but I got a problems of unexpected end but I dont understand why
def multiple_of_3_or_5(nb)
if nb % 3 == 0 || nb % 5 == 0
return true
else
return false
end
end
def perform
i = 1
res = 0
while i <= 1000
if multiple_of_3_or_5(i)
res += i
end
i++
end
puts res
end
perform
I got those errors in iTerm :
multiples.rb:17: syntax error, unexpected `end'
multiples.rb:21: syntax error, unexpected end-of-input, expecting `end'
But when I remove the end l.17 I got this
multiples.rb:18: syntax error, unexpected local variable or method, expecting `do' or '{' or '('
puts res
Can anyone explain my why and how to do for this programs works?
Ok I thought in ruby I can do i++ but i have to i += 1. That's work now.
Related
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
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
This question already has answers here:
Unexpected keyword_end error, yet syntax seems fine
(2 answers)
Closed 8 years ago.
I am getting an unexpected keyword_end error and I don't know why:
def add(meeting)
if conflict?(meeting)
puts "There's conflict with another meeting!"
else
if #meetings.empty?
#meetings.push(meeting)
else
i = 0
#meetings.each do |m|
if m > meeting
#meetings.insert(i, meeting)
break
end
i++
end
end
end
end
If I delete the each loop there is no problem. I have tried with a while loop and with an until loop and I am getting the same error in all of them.
Can someone explain what I am doing wrong?
There's no unary operator ++ in Ruby. You should have:
i += 1
In line 14, you are adding two numbers, but you forgot to pass the second number, instead the parser is encountering an end. So, the error message is slightly misleading: it's not that the parser isn't expecting an end there, rather it is expecting to see something before the 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
A simple CLI program. I am taking the input from user about ingredients until user enters nothing. Joining all the ingredients by a comma and returning it.
ingredient = []
int i = 0
ingredients = ""
puts "Enter ingredients :"
loop do
puts "Ingredient #{i+1}"
ingredient[i] = gets.chomp
break if ingredient[i] = ""
i++
end
ingredients = ingredient.join ","
puts ingredients
I don't know what's wrong with this but I keep getting the error "syntax error, unexpected end-of-input, expecting keyword_end". Please help.
ingredient = []
i = 0 # No int
ingredients = ""
puts "Enter ingredients :"
loop do
puts "Ingredient #{i+1}"
ingredient[i] = gets.chomp
break if ingredient[i] == "" # == in comparison
i+=1 # NOT i++
end
ingredients = ingredient.join ","
puts ingredients
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