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
Related
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 migrating a project from Ruby 1.8.7 to Ruby 2.3 and rails from 2 to 4.
I have this code which was working in 1.8.7
class_list = []
original_mechanism = ActiveSupport::Dependencies.mechanism
ActiveSupport::Dependencies.mechanism = :load
begin
class_list += load("/tmp/abc.rb")
rescue Exception => e
debug e.backtrace
end
ActiveSupport::Dependencies.mechanism = original_mechanism
my sample file abc.rb
class Abc
def ...
end
class Def
...
end
In Ruby 1.8.7 class_list is [Abc,Def]
In Ruby 2.3 class_list is [true] - (I had to change the line to 'class_list << load("/tmp/abc.rb")' to make it give this output )
Any of you know how to make it return the old way? All I want is the ClassNames of the classes loaded at the end.
This is not Ruby. Ruby's load always returned true, both in 1.8.7 and 2.3.1. However, there's ActiveSupport load that uses load_dependency (http://apidock.com/rails/v3.0.0/ActiveSupport/Dependencies/Loadable/load_dependency) which does return new constants defined in the file.
Can you try to use load_dependency instead of load here?
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
In Ruby it's easy to see if a module is defined:
defined? MyModule
But in Rails, when you do that Rails tries to load the module (from autoload paths, etc.). So if it does not exists, it throws an error instead of returning false.
I could do
do
defined? MyModule
rescue
# false
end
But it there a better way ?
What version of Ruby and Rails are you using? When I try this, no error is thrown:
ruby-1.9.3-p0 :1 > defined? Rails
=> "constant"
ruby-1.9.3-p0 :2 > defined? NotAConstant
=> nil
There is a module
require 'iconv'
module Escape
def escape(string)
return_value = Iconv.conv('ascii//translit//IGNORE', 'utf-8', string).to_s
end
end
It`s work in 1.8.7 but not in 1.9.1
The error message is "NameError (uninitialized constant Escape::Iconv)"
and the follow is work in 1.9.1,Why??????? (my rails is rails 3 in ubuntu)
module Escape
def escape(string)
require 'iconv'
return_value = Iconv.conv('ascii//translit//IGNORE', 'utf-8', string).to_s
end
end
don't use 1.9.1 for rails3, instead use 1.9.2 or 1.8.7 . read here in the comments: http://weblog.rubyonrails.org/2010/2/5/rails-3-0-beta-release