I am trying to run the following simple method in my local environment, and I cannot figure out what is wrong with it
def test_counter(max)
i = 0
count = 0
while count < max
if i != 0
count = i**2
i++
end
end
puts i
end
test_counter 50
I get these errors:
practice.rb:9: syntax error, unexpected keyword_end
practice.rb:35: syntax error, unexpected end-of-input, expecting keyword_end
There is no i++ in ruby; use i += 1. Also i will never equal something beside 0 so if i != 0 does not work
Related
What's wrong with this block?
if item.nil?
found = true
elsif item[:name] == name && item[:category] == category
found = true
else
i++
end
When I do syntax checking, I get
syntax error, unexpected keyword_end
but if I delete end then I get
syntax error, unexpected end-of-input
The problem is here:
i++
Ruby doesn't have a ++ operator. What you want is this:
i += 1
I believe the reason for that specific error is that Ruby is interpreting the second + as a unary + operator, i.e. the "positive number sign" (because it's the only thing that makes sense in that context), and so expects the next token to be a number†, e.g. (parentheses for clarity):
i + (+5)
...but the next token is end, ergo the syntax error.
†Or something else that responds to the +# method.
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.
I get:
Untitled 5.rb:16: syntax error, unexpected end-of-input, expecting
keyword_end
even though I closed the if/else statement and def with end:
def max_2_sum(int_collection)
sum = 0
if int_collection.empty?
sum
else if int_collection.count == 1
int_collection.first
else
sum = int_collection.sort[-1] + int_collection.sort[-2]
end
end
int_collection = [4,3,9]
puts max_2_sum(int_collection)
else if
is wrong. It should be
elsif
your code should be like below
def max_2_sum(int_collection)
sum = 0
if int_collection.empty?
sum
elsif int_collection.count == 1
int_collection.first
else
sum = int_collection.sort[-1] + int_collection.sort[-2]
end
end
This is what's up:
It seem I've got a syntax problem. Both of the following methods, which are similar to one another, load to irb fine. When I go to use them, though, they produce errors. Since I'm just learning this, I'm not only looking for a fix, I'm looking to understand why both return errors and how any fix works to solve the issues.
Thanks for the help,
TJ
First method:
def sum(*)
i = 1
total = sum[0]
until sum[i] == nil
total = total + sum[i]
i += 1
end
puts total
end
Upon loading this file irb returns => nil, no errors. However, an attempt to use the method looks like:
sum 3, 58, 298, 2
Unknown error.
Writing the method directly in irb vs loading .rb file produces this error after line 3:
def sum(*)
.. i = 1
.. total = sum[0]
(eval):2: (eval):2: compile error (SyntaxError)
(eval):2: syntax error, unexpected $end, expecting kEND
total = sum[0]
^
Now, I see that it says SyntaxError but I do not know what the rest of the feedback means nor what proper syntax for this would be.
Second method:
def sum(*)
i = 1
total = sum[0]
until i == sum.count
total = total + sum[i]
i += 1
end
puts total
end
This one produces all the same errors in the same ways as the first.
Try this instead:
def sum(*nums)
sum = 0
nums.each { |num| sum += num }
sum
end
sum(1, 2, 3)
I don't think `def sum(*) is valid Ruby syntax; you have to give your vararg a name.
Also, see this Stackoverflow question for an even shorter way to sum an array of numbers.
Quick question, why this doesn't work?
sum = {}
e = "a"
(sum[e] ||= 0) += 1
I got (expecting 1):
SyntaxError: unexpected tOP_ASGN, expecting $end
(sum[e] ||= 0) += 1
When you do (sum[e] ||= 0) the parentheses cause the expression to be evaluated. As a result, it'll evaluate to the value of sum[e] (e.g. 0), which you cannot do a += operation on. It won't return an lvalue/reference to sum[e] as you might expect.
You can verify this by doing the same operation on what would be the result:
irb(main):001:0> 0 += 1
SyntaxError: (irb):1: syntax error, unexpected tOP_ASGN, expecting $end
Or even something like this:
irb(main):004:0> (sum[e]) += 1
SyntaxError: (irb):4: syntax error, unexpected tOP_ASGN, expecting $end
If for some reason you want to keep it on one line, you can do something like this:
sum[e] ||= 0; sum[e] += 1
As dbenhur mentioned, if you are going to be doing this a lot, you can also set a default value for all keys in the hash:
sum = Hash.new(0)
Sets sum to be a hash where every key is automatically set to 0 when you access it.