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.
Related
This question already has an answer here:
Ruby: unexpected end-of-input, expecting keyword_end for if statement
(1 answer)
Closed 1 year ago.
I'm attempting to follow a Ruby tutorial, but I'm getting a situation where my code returns an error for my while block.
If I include 'end' at the end of my while block, like so:
while attempts < 3
puts "Please input your username."
usernameTest = gets.chomp
puts "Please input your password."
passwordTest = gets.chomp
attempts++
end
It returns with syntax error, unexpected end'`.
However, if I remove that last end statement, it returns with syntax error, unexpected end-of-input. So it seems to be unable to make up its mind whether it wants that end statement or not.
attempts++ isn't valid in Ruby, you need to use attempts += 1 instead
This question already has answers here:
Why does white-space affect ruby function calls?
(2 answers)
Closed 6 years ago.
I am testing a method with multiple arguments. For some reason, Ruby will run fine if I have just one argument to the calculate() method, but when I add a second, it causes an unexpected end of input error.
This is the code:
def set_weights
actual_weight = #desired_weight.to_i - 45
calculate (actual_weight, 65)
end
def calculate (remaining_weight, plate_weight)
end
The error message is:
weights.rb:31: syntax error, unexpected ',', expecting ')'
calculate (actual_weight, 45)
^
If I remove the second argument, I get no errors.
def set_weights
actual_weight = #desired_weight.to_i - 45
calculate (actual_weight)
end
def calculate (remaining_weight)
end
Define a function:
irb(main):012:0> def add(x, y) x+y end
`=> nil
If you call it without a space between the arguments and the function:
irb(main):013:0> add(5,6)
=> 11
With the space:
irb(main):014:0> add (5,6)
SyntaxError: (irb):14: syntax error, unexpected ',', expecting ')'
add (5,6)
^
from /usr/bin/irb:12:in `<main>'
The extra space before the argument list causes the interpreter to throw a SyntaxError. Since ruby functions can be run with or without parens, including a space makes the interpreter think it is about to receive arguments for the function - instead it receives a tuple (5,6).
Remove the space and your code will run correctly.
Remove the extra space before the method call, like this:
def set_weights
actual_weight = #desired_weight.to_i - 45
calculate(actual_weight, 65)
end
def calculate (remaining_weight, plate_weight)
end
I'm trying to complete an exercism.io test file which compares two strings and adds one to a counter each time there is a difference between the two strings. I've written my class, but for some reason it won't run in terminal. I've compared my code with several examples of syntax online and don't see why it won't run. Any help would be much appreciated.
Here's my class:
class Hamming
def compute(str1, str2)
distance = 0
length = str1.length
for i in 0..length
if str1[i] != str2[i] then
distance++
end
end
return distance
end
end
And here's a relevant bit of test file:
class HammingTest < Minitest::Test
def test_identical_strands
assert_equal 0, Hamming.compute('A', 'A')
end
end
Lastly, here's the error I'm getting:
hamming_test.rb:4:in `require_relative': /Users/Jack/exercism/ruby/hamming/hamming.rb:8: syntax error, unexpected keyword_end (SyntaxError)
/Users/Jack/exercism/ruby/hamming/hamming.rb:12: syntax error, unexpected end-of-input, expecting keyword_end
from hamming_test.rb:4:in `<main>'
You don't need then after condition in if statement.
Use two spaces instead of four for indentation in Ruby.
(Direct cause of your error) there's no ++ operator in Ruby. You should have
distance += 1
I was working with Java for a few months and am transitioning back to Ruby now. I am getting a weird error from the following code:
def count_divisors
divisor_hash = {}
25.times do |i|
divisor_hash[i] = find_dividends(i)
end
puts divisor_hash
end
def find_dividends(i)
count = 0
1000.times do |k|
if i % ( k + 1 ) == 0
count++
end
end
count
end
count_divisors()
This code is generating the following error:
test.rb:14: syntax error, unexpected keyword_end
test.rb:19: syntax error, unexpected end-of-input, expecting keyword_end
This error does not occur when I remove the if statement. I am not sure why. I know every if statement needs an end statement, for some reason it seems upset by the placement of that end statement though.
Change the count++ to count += 1 Ruby does not support incremental operator.
This question already has answers here:
Unexpected keyword_end error, yet syntax seems fine
(2 answers)
Closed 8 years ago.
I seem to be getting an error for this block, and I'm not quite sure why. If I remove the break and the counter it works, but if I add them I get this error:
Error:
/home/rails_apps/Twitter_App/app/controllers/dashboard_controller.rb:133: syntax error, unexpected keyword_end
/home/rails_apps/Twitter_App/app/controllers/dashboard_controller.rb:145: syntax error, unexpected end-of-input, expecting keyword_end
Code:
#followers2.each do |follow|
#followers3 << Twitter.user(follow)
break if i >10
i++
end
I was an idiot, I totally forgot that Ruby doesn't make use of the increment operator....doh!
Changed from:
#followers2.each do |follow|
#followers3 << Twitter.user(follow)
break if i >10
i++
end
To this:
#followers2.each do |follow|
#followers3 << Twitter.user(follow)
break if i >10
i+=1
end
On the last line you are using the binary infix + operator but you never provide the second operand. Ruby is expecting the operand on the next line (whitespace is allowed between an operator and its operands) but instead it hits the end keyword. You need to provide the second operand.