I need to use XmlMarkup.
In my script, I import "builder" but when I create an element I receive the error "uninitialized constant Builder (NameError)". Here the step that fails:
require 'rubygems/builder'
...
xml = Builder::XmlMarkup.new( :indent => 2) ===> uninitialized constant Builder (NameError)
I tried also using other syntax, like:
::Builder::XmlMarkup.new( :indent => 4 )
but I received the same error
Write as below, as Usage is telling :
require "builder" # when your ruby version is 1.9 or greater.
Uninitialized name error means you spelled the class wrong. XmlMarkup is case sensitive
In my case i had to do:
require "builder/xmlmarkup"
Related
If I follow the following part of this article:
Figure out where a method was defined
object = Object.new
puts object.method(:blank?).source_location
=> ["/gems/activesupport-5.0.0.beta1/lib/active_support/core_ext/object/blank.rb", 14]
I should be able to find the definition of the blank? method, however when I try this code within irb with ruby 2.0.0 I get this error message:
➜ ~ irb
irb(main):001:0> object = Object.new
=> #<Object:0x007fc84882f088>
irb(main):002:0> puts object.method(:blank?).source_location
NameError: undefined method `blank?' for class `Object'
from (irb):2:in `method'
from (irb):2
from /usr/bin/irb:12:in `<main>'
Did I miss anything?
Thank you.
.blank? method does not exist for a Object type. I know for sure it exists for a String method if I include the active_support lib
irb(main):001:0> String.new.method(:blank?).source_location
=> ["/home/xeon/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object/blank.rb", 116]
If you include activesupport-5.0.0.beta1 then it will work for you. (Looking at the source path of the article you have posted)
ruby-v2.2.3 is supposed to have Date class preloaded into irb, however when I enter...
Date
NameError: uninitialized constant Date
from (irb):1
from /Users/noah/.rubies/ruby-2.2.3/bin/irb:11:in `'
Why should I have to require Date every single time if it's supposed to be preloaded into 2.2.3?
Date isn't listed as a core class in v2.2.3 or the current Ruby v2.3.1 core-classes, but Time is. Here's some IRb output:
$ irb -f
irb(main):001:0> Date.class
NameError: uninitialized constant Date
Did you mean? Data
from (irb):1
from /Users/ttm/.rbenv/versions/2.3.1/bin/irb:11:in `<main>'
irb(main):002:0> Time.class
=> Class
irb(main):003:0> Time.methods(false)
=> [:at, :now, :utc, :gm, :local, :mktime]
That is a limited subset of Time's methods though:
irb(main):002:0> require 'time'
=> true
irb(main):003:0> Time.methods(false)
=> [:at, :now, :utc, :gm, :local, :mktime, :parse, :zone_offset, :strptime, :rfc2822, :rfc822, :httpdate, :xmlschema, :iso8601]
Why do you say Date is preloaded? It's not a core class, it's part of the stdlib so it needs to be required. Time is a core class instead.
You can try to do the following in the beginning of your file:
require 'Date'
As far as I know, (and as Ursus said), Date is not preloaded.
Attention: So Tilec suggested to just load the library at the beginning of the file, which gave me (Not sure if that is true in general): this gave me LoadError: cannot load such file -- Date. Trying gem install Date gives me
ERROR: Could not find a valid gem 'Date' (>= 0) in any repository
ERROR: Possible alternatives: date
Solution: So I'm proposing to correct to lower case:
require 'date'
I try to upgrade a legacy application from Ruby 1.8.7 to 2.2.3. Afterwards the rendering of builder templates raises errors about unknown classes.
uninitialized constant Builder::XmlMarkup::BigDecimal (NameError)
It seem that within the Builder::XmlMarkup constants like classes disappear.
### example.xml.builder (template) ###
BigDecimal.new('23') # no error
class << xml
def some
data(BigDecimal.new('23')) # raises an error in 2.2.3
end
end
xml.test { xml.some }
### main script ###
require 'rubygems'
require 'builder'
require 'bigdecimal'
def eval_script(file)
xml = Builder::XmlMarkup.new
binding.eval(File.read(file), file)
xml.target!
end
template = File.join(File.dirname(__FILE__), 'example.xml.builder')
puts eval_script(template)
# Ruby 1.8.7 / builder 3.2.0 => <test><data>0.23E2</data></test>
# Ruby 2.2.3 / builder 3.2.2 => ./eval_script.rb:5:in `some': uninitialized constant Builder::XmlMarkup::BigDecimal (NameError)
I found no reason for the behavior. How can I fix the problem?
BTW: I have the same problem with the method lookup, e.g format('%d', 42) which returns the full XML document but doesn't call Kernel.format in Ruby 2.2.3.
I found a workaround overriding const_missing which has to be applied to every template file. It works so far for the legacy application.
### example.xml.builder (template) ###
class << xml
def self.const_missing(name)
super rescue ::Object.const_get(name)
end
def some
data(BigDecimal.new('23'))
end
end
xml.test { xml.some }
But every time the constant BigDecimal is used, it triggers const_missing and then raises a NameError and calls the Object method.
Does anyone know what I'm doing wrong here, I installed Ruby 2.1, Sequel-4.26.0 gem and mislav-will_paginate-2.3.10 gem, but when i try to use the paginate function, i keep getting the following error:
Code:
#user = User.paginate(:page => 1, :per_page => 2)
Error message:
"ERROR: NoMethodError: undefined method `paginate' for #"
Most likely paginate is a dataset method, not a class method (this is true for Sequel's pagination extension, not sure about will_paginate). If you want User.paginate to work:
def User.paginate(*args)
dataset.paginate(*args)
end
I have this problem:
uninitialized constant Couch::Couchbase (NameError)
./features/step_definitions/lib/couchbase.rb:6:in `get'
./features/step_definitions/StepsLib.rb:130:in `/^I get couch$/'
features/test.feature:4:in `Then I get couch
The code is:
require 'rubygems'
require 'couchbase'
class Couch
def get
client = Couchbase.connect(:bucket => "user", :hostname => "192.168.1.50")
user = client.get("COMMENT-FO-1103")
return user
client.disconnect
end
end
I've been loocking all over, and no clue, I'm no ruby expert.
Thanks.
Try connecting through IRB and see if you get the same error.
Open up an IRB instance and type:
> require 'Couchbase'
You'll get a statement back saying: => true
Then connect as follows:
> c = Couchbase.new("http://localhost:8091/pools/default/buckets/MyBucket")
This should connect you directly to the bucket you wish to operate on.
Then try:
> c.set("mykey", "Some Value")
And you should get a confirmation that the object has been set in the Bucket.
Then use:
> c.get("mykey")
And you should print the value of the object you just set.
Regarding your code above, I'm not sure why exactly you're trying to wrap this call up in a Class? What is your Use case?
I notice on the github page that methods like get aren't run on client itself, but instead are used in a block argument to run like:
client.run { |conn| conn.get("COMMENT-FO-1103") }
That's really all I can think of there. Hope it helps.
I do notice that return user will prevent the line client.disconnect from ever running, as return kicks you out of the method entirely.