I am getting syntactic errors in ruby files in my system although these are not there in other systems.
The errors are in code like:
1) redirect_to :back, alert: exception.message
The syntactic error is in alert
2) load_and_authorize_resource only: [ :update, :destroy ]
The syntactic error is in only
3) render json: #reward.to_json
The syntactic error is in json
Like that there are number of errors.
I am getting rid of these errors by doing following changes to the above:
1) redirect_to :back, :alert => exception.message
2) load_and_authorize_resource :only => [ :update, :destroy ]
3) render :json => #reward.to_json
It seems that error is due to ruby version but I am not sure what's the proper reason is?
I need to do all such changes in every existing projects and is quite painful.
Does that imply that the projects are using old ruby syntax or does that imply that I have old ruby version installed?
My ruby version is ruby 1.9.2p180
Also if its ruby version problem than does the ruby upgrade to the latest version will affect the whole project and how to accomplish this easily ?
UPDATE:
I confirmed that my ruby version is ruby 1.9.2p180 because of the following:
D:\ruby_work>ruby -v
ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
D:\ruby_work>pik list
187: ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]
* 192: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
The asteric(*) above is indicating the version I am currently using.
The exact errors are for example in redirect_to :back, :alert => exception.message is-
, unexpected ':'
UPDATE:
Now I did -
D:\ruby_work>pik use 187
D:\ruby_work>pik list
* 187: ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]
192: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
D:\ruby_work>ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]
The error is still there. Now I suspect Is it really a ruby error because changing to older ruby version also showing the same syntactic error. The only difference is that now even changing from :key to key => is also showing the same error.
Older versions of Ruby didn't understand the hash notation of key: value, only :key => value, which 1.9+ understands.
I don't remember when the new notation was added, but, as you found, the fix to allow the code to run on older versions is to use the original notation.
In Ruby 1.8.7, this is the error I get using IRB:
irb(main):001:0> foo = {a:'b'}
SyntaxError: compile error
(irb):1: syntax error, unexpected tSYMBEG, expecting kDO or '{' or '('
foo = {a:'b'}
^
(irb):1: syntax error, unexpected '}', expecting $end
from (irb):1
irb(main):002:0> foo = {:a => 'b'}
=> {:a=>"b"}
Related
I'm getting an odd error, where no rails commands of any kind are executing. Whenever I enter a rails command I get the following:
Ignoring racc-1.5.2 because its extensions are not built. Try: gem pristine racc --version 1.5.2
(this is repeated about 100 times before the following):
bin/rails:2:in `load': /Users/robertmorris/Desktop/All Projects/Code School/my9s/bin/spring:11: syntax error, unexpected keyword_rescue, expecting keyword_end (SyntaxError) rescue Gem::LoadError ^ /Users/robertmorris/Desktop/All Projects/Code School/my9s/bin/spring:14: syntax error, unexpected keyword_end, expecting end-of-input from bin/rails:2:in `<main>'
My bin/spring file:
if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"])
gem "bundler"
require "bundler"
# Load Spring without loading other gems in the Gemfile, for speed.
Bundler.locked_gems&.specs&.find { |spec| spec.name == "spring" }&.tap do |spring|
Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
gem "spring", spring.version
require "spring/binstub"
rescue Gem::LoadError
# Ignore when Spring is not installed.
end
end
Thanks!
You are using an old (unsupported) version of ruby.
You should upgrade it using any available local version manager like rvm, rbenv or asdf, to at least 2.5, or better 2.7 or 3.
On ruby 2.4 and previous, you could only rescue inside either a method:
def foo
# do something
rescue
# do something else
end
or an explicit begin/end block:
begin
# do something
rescue
# do something else
end
Since ruby 2.5 was released, all do/end blocks can have rescue clauses:
foo.bar.baz.tap do
# do something
rescue
# do something else
end
while before that you had to
foo.bar.baz.tap do
begin
# do something
rescue
# do something else
end
end
I come across this in Rails source code:
class Object
def duplicable?
true
end
end
class NilClass
begin
nil.dup
rescue TypeError
def duplicable?
false
end
end
end
With this code, even after dup is removed from an object, that object responds to duplicable? with true.
I think it can be rewritten to a simpler code like:
class Object
def duplicable?
repond_to?(:dup)
end
end
What is the merit of defining duplicable? using begin...rescue?
What is the merit of defining duplicable? using begin...rescue?
Ruby before 2.4 raised a TypeError when attempting to nil.dup:
$ rbenv local 2.3.0
$ ruby --version
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
$ ruby -e 'p nil.dup'
-e:1:in `dup': can't dup NilClass (TypeError)
from -e:1:in `<main>'
Starting with Ruby 2.4, nil.dup just returns itself:
$ rbenv local 2.4.0
$ ruby --version
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin15]
$ ruby -e 'p nil.dup'
nil
Putting the method definition inside rescue ensures that the method is only defined for Ruby versions which raise the TypeError.
I think it can be rewritten to a simpler code like: [...]
Simply checking whether the receiver responds to dup doesn't work, because nil – being an Object – does respond to dup, even in 2.3. The TypeError is (was) raised from within Object#dup:
VALUE rb_obj_dup(VALUE obj)
{
VALUE dup;
if (rb_special_const_p(obj)) {
rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
}
// ...
}
nil responds to dup explicitly throwing the TypeError (which has, in turn, nothing to do with NoMethodError.) [Correction: had responded to dup before 2.4, credits go to #Stefan.]
NilClass.instance_method(:dup)
#⇒ #<UnboundMethod: NilClass(Kernel)#dup>
The goal is to respond to duplicable? with false unless NilClass#dup is overwritten by another monkey patcher in the city. [Correction: read “another monkey patcher” as “Matz” :)]
I am new to ruby. I keep getting undefined method 'valid_xml'? What am I doing wrong?
require 'rexml/document'
include REXML
begin
good_xml = %{
<groceries>
<bread>Wheat</bread>
<bread>Quadrotriticale</bread>
</groceries>}
puts(good_xml)
valid_xml?(good_xml)
puts("good read")
rescue Exception => e
puts(e.message)
end
and here is the output
-bash-4.1$ ruby test.rb
<groceries>
<bread>Wheat</bread>
<bread>Quadrotriticale</bread>
</groceries>
undefined method `valid_xml?' for main:Object
Here is the ruby version I have
ruby 1.8.7 (2009-12-24 patchlevel 248) [x86_64-linux]
valid_xml? is not defined in REXML (and neither did you define it). You could define it like this:
def valid_xml?(xml)
REXML::Document.new(xml)
true
rescue REXML::ParseException
false
end
I'm in the middle of the upgrade of my application. I want to move from ruby 1.8 to 1.9 and from rails 3.0 to 3.2 (right now I run ruby 1.9 and RoR 3.2). I have problems with my tests: the output is vague and not very helpful. For example for assert_difference I get:
Failure:
test_should_create_new_version_if_versioning_needed_and_diaconies_changed(AnimatorProfilesControllerTest)
test/functional/animator_profiles_controller_test.rb:108:in `block in <class:SomeTest>'
=> 108: assert_difference('SomeModel.count', 1) do
109: put :update, :id => #model.to_param, :model => attrs
110: end
111:
<> expected but was
<>
diff:
nil
The problems is that there is no diff (as you can see). The other problem is that when I have a message in assert, it is not shown when it fails. How could I fix it?
I don't know why I get this error. Both methods are corrects.
hydra = Typhoeus::Hydra.new
hydra.cache_getter do |request|
Rails.cache.read(request.cache_key) rescue nil
end
hydra.cache_setter do |request|
Rails.cache.write(request.cache_key,request.response, expires_in: request.cache_timeout)
end
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin11.4.2]
Rails 3.2.8
You are getting that error because those methods do not exist on an instance of Typhoeus::Hydra. You can find the full list of available methods at http://rubydoc.info/gems/typhoeus/0.5.0/frames
Update
Those methods were removed between versions 0.4.2 and 0.5.0 via this commit