When I use a shortened if/else statement with Sinatra commands, I receive a syntax error:
request.cookies['blog'].nil? ? erb :blog : redirect '/done'
Returns this error:
/home/sinatra/ptt/ptt.rb:107: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '(' request.cookies['blog'].nil? ? "erb :blog" : redirect '/done' ^
Both of the statements produce errors when by themselves (without Sinatra code as the other statement).
Is this a Sinatra problem or is the syntax incorrect?
The error does not occur when the true/false statements are plain Ruby:
request.cookies['blog'].nil? ? foo = 1 : bar = 2
I think Ruby can't decide what is a method call and what belongs to the if statement. Try this:
request.cookies['blog'].nil? ? erb(:blog) : redirect('/done')
Related
I have this very simple piece of code (using Ruby 3)
def eat(main, dessert*)
if dessert.empty?
puts "I eat #{main}"
else
puts "I eat #{main} than #{dessert}."
end
end
Wher I run eat("mushrooms") that provokes errors:
argu.rb:1: syntax error, unexpected '*', expecting ')'
def manger(plat, dessert*)
argu.rb:7: syntax error, unexpected `end', expecting end-of-input
I don't see why.
Splat operator should put before parameters so your signature should be
def eat(main, *dessert)
Not sure where you got the idea from using dessert*, but you could define your method as
def eat(main, dessert = [])
to provide a default argument (of course it must be one which can respond to empty?).
Of course it is up to you to justify, why "main" can be anything (i.e. a String), but dessert must be a collection. I would test for dessert as
if dessert.nil?
and hence provide nil as default value for the dessert.
I'm baffled why I have a syntax error using '&&' when combining two conditions that should be true before the program can continue. I'm new to ruby and have not had a chance to actually code a complete program by myself.
I am just updating an old program and following through whatever code that is similar to what I need to update it with.
Here's my code:
def drug_dispensation_condition(charge)
return true if ['250','251'].include? charge[:rev_code]
return true if ['981262'].include? charge[:cdm_code]
return true if ['300'].include? charge[:rev_code] && ['86580'].include? charge[:cdm_code] #this line is inserted
false
end
Here's my error message:
syntax error, unexpected tIDENTIFIER, expecting keyword_end
(syntaxerror) ...e] && ['86580'].include? charge[:cdm_code]
I added the 3rd "return true" clause to include the rev_code 300 and the cdm_code 86580.
I need both the rev_code 300 and the cdm_code 86580 to declare the statement as True, so I needed to add the "&&" in between them. It looked like it was expecting the "end" after "charge[:rev_code]" instead of the 2nd condition.
Is there any other way of coding this to avoid the syntax error?
As it's a one-line statement, wrap the include? arguments in parenthesis:
return true if ['300'].include?(charge[:rev_code]) && ['86580'].include?(charge[:cdm_code])
I think you could make it a little bit more readable:
def drug_dispensation_condition(charge)
if ['250', '251', '300'].include?(charge[:rev_code]) ||
['981262', '86580'].include?(charge[:cdm_code])
return true
end
false
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
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.
In Settingslogic fork allowing array as source, in ruby 1.8.7 everything is working, but in ruby 1.9.2 there is an error. The problem is within this part of the code:
self.class.class_eval <<-EndEval
def #{key}
return ##{key} if ##{key}
raise MissingSetting, "Missing setting '#{key}' in #{#section}" unless has_key? '#{key}'
value = fetch('#{key}')
##{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{#section}") : value
end
EndEval
#section == ["path_to_yml_file1", "path_to_yml_file2",...]
Looks like #{} is evaluated in some strange way, "#{#section}" seems to be an array, not a string. Can anybody explain this?
Error trace:
#section == ["User/project/config/defaults.yml", "/Users/project/config/development.yml"]
ruby-1.9.2-p290 :001 > Settings.keys
SyntaxError: (eval):3: syntax error, unexpected tSTRING_BEG, expecting keyword_end
...project/config/defaults.yml", "/Users/project...
... ^
(eval):3: syntax error, unexpected tSTRING_BEG, expecting keyword_end
...project/config/development.yml"]" unless has_key? 'front'
... ^
(eval):5: syntax error, unexpected tSTRING_BEG, expecting ')'
...project/config/defaults.yml", "/Users/project...
... ^
(eval):5: syntax error, unexpected tSTRING_BEG, expecting keyword_end
...project/config/development.yml"]") : value
... ^
(eval):5: syntax error, unexpected ')', expecting keyword_end
...project/config/development.yml"]") : value
... ^
from .../settingslogic-3b5d7d9cc319/lib/settingslogic.rb:198:in `class_eval'
Thanks for any help
You've made a fork from main settingslogic. At that time it didn't support array as source, but now it does. Try to use main settingslogic repository.
Your error now related to this string:
raise MissingSetting,
"Missing setting '#{key}' in #{#section}" unless has_key? '#{key}'
because in case of using array instead of string
./settings.yml
you get something like this:
[\"./settings.yml\"]
The same happens with ##{key} assignment below. In main repository this code replaced to string concatenation.
Try self.class_eval or even without self, no need to get the name of class and self automatically assign to current object i.e. your class.