I feel pretty dumb having to ask this, but it's had me stumped way too long. Attempting to run the following, I get
finance.rb:1: syntax error, unexpected kDEF, expecting $end
def get_sign(input)
return "+" if input.include? "+"
return "-" if input.include? "-"
end
def get_account_name(input)
if input.split[0] == "new"
return input.split.reject{|x| x == "new" or x == "account"}[0]
else
return input.split[0]
end
end
If I wrap them in a class, it just expects <, \n, or ; instead of $end.
Ruby 1.8.7 and 1.9 (via Macruby) give the same error. Removing the second method seems to get it working. Someone, please enlighten me; this seems like a really fundamental misunderstanding of something on my part.
I don't get this error if I copy/paste your contents from this question into a new file. If you do so (copy/paste from this web page into a new file and save that) do you still get the error? If not, perhaps you have some garbage whitespace in your file?
Added as answer because another user may find this question with a similar problem and not read through all the comments to find the solution.
Related
I am new to Ruby. I am looking for an elegant one line solution to this line of code:
puts "Year: ".colorize(:light_blue) + "#{instrument.year}"
I would like it to print "N/A" if instrument.year is "" (an empty string). I imagine an unless, if or || might work, but so for nothing has quite worked for me. Still learning! I have several fields that need this treatment, so I'd like to avoid an if/else/end statement.
Thanks in advance!
I'm not sure what exactly you want but what I understood is you want to display instrument.year or N/A if instrument.year is an empty string.
So what I would do is use a ternary:
puts "Year: ".colorize(:light_blue) + "#{instrument.year.empty? ? 'N/A' : instrument.year}"
I am attempting to modify someone's script.
I have managed to modify everything but there is one problem left I am unable to solve:
disp_status("\tAnswer: #{convert_err(results["status"])}")
This produces various outputs as it is run, however, when the output is "ERROR", I want it to do an action. I am not sure how to limit it to "Error", as it appears to always run the method no matter the output.
What I tried was:
if #{convert_err(results["status"])} =~ /ERROR/
and a lot of similar iterations without any luck. Can anyone help?
In ruby interpolation doesn't work without double-quotes. But using interpolation here is an over kill, so just change the line in question from:
if #{convert_err(results["status"])} =~ /ERROR/
To
if convert_err(results["status"]) =~ /ERROR/
And should it should work! :-)
I think the .include? method might be helpful. You could do:
if "#{convert_err(results["status"])}".include?("ERROR")
Furthermore if convert_err returns a string you could just call:
if convert_err(results["status"]).include?("ERROR")
And another option would be to call .to_s which will convert the result of convert_err to a string. So that would look like:
if convert_err(results["status"]).to_s.include?("ERROR")
For further reference read: http://www.ruby-doc.org/core-2.1.4/String.html#method-i-include-3F
I have the following code:
address = "#{(article/"div.address").inner_html.strip_html.squish}"
(using Hpricot)
And in some instances...
address = "#{(article/"div.address").inner_html.strip_html.squish}"
...is nil
I would like the script to keep chugging along, possibly replacing nil with an empty string.
Any tips?
Edit
I have traced the problem better to:
puts "#{link[0].to_s}\n" unless link.empty?
(.backtrace points to this particular line in the source.)
So the revised question is: why doesn't that line just not get parsed? Why does it throw an error? I thought that using unless will just skip it...
Use :to_s method:
nil.to_s == ''
Is try what you are looking for? http://api.rubyonrails.org/classes/Object.html#method-i-try
Thank you all for the support and helpful tips, in the end it was a matter of using the proper method, I ended up solving my problem by using:
unless uri.query.nil?
But I did come to make use of both .to_s and try in my source, and I wish I could pick two answers as the right one!
I'm having a lot of trouble with Ruby after coming back to it from a long break.
I'm getting a lot of 'unexpected kEND' errors, and I've tracked it down to lines below. I'm not having trouble with a particular piece of code, but rather, the concept of 'unexpected kEND' .
if (condition)
do-one-line-thing()
and
# inside of a loop...
if ( condition-evaluation-that-might-cause-error-during-run-time )
do-something()
end
and
myarray.each { |element|
do-soemthing-that-might-cause-error-during-run-time-for-some-but-not-all-values()
}
Question :
What other things can cause these kEND errors ? It seems like kEND is being used as a general "Badness on line ##" error? What can you tell me about kEND errors in general?
an unexpected kEND is where the end keyword was found somewhere it shouldn't be.
Generally you've closed too many code blocks, or you've got some other syntax problem.
If you paste a (complete) file which has this problem we can point out the error...
There is a syntax error in the first piece of code
if (condition)
do-one-line-thing()
You always have to explicitly close the if clause with end. You cannot omit it, like you do in many other languages, even if the block consists of a single line.
Just tried to execute a small Lua script, but unfortunately I'm doing something wrong. I've no more ideas what the fault might be.
function checkPrime( n )
for i = 2, n-1, 1 do
if n % i == 0 then
return false
end
end
return true
end
The interpreter says:
lua: /home/sebastian/luatest/test.lua:3: `then' expected near `%'
I think it's not a big thing and perhaps it's quite clear what is wrong. But somehow I cannot see it at the moment.
There is probably some version problem, check your version of lua. The usage of '%' as an infix operator for modulo can only be used in Lua 5.1, in 5.0 it is not supported yet. Try using math.mod instead:
if math.mod(n,i) == 0 then
Edit: Also note that in 5.1, math.mod still exists, but it has been renamed to math.fmod. For now, the old name still works, but support will probably be removed in future versions.
Have you tried wrapping "n% i == 0" in parentheses? Stupid question, but sometimes overlooked!