I'm using Ruby 1.9.3, and when I execute Date.today, I get NoMethodError: undefined methodtoday' for Date:Class`
I'm pretty confused about this since it does appear to be in the documentation. Though I know this is the documentation for 2.0, but I see answers ranging back to when it was implemented in 1.8.7. Was this removed in 1.9?
Did you require 'date' ?
> require 'date'
=> true
> Date.today
=> #<Date: 2013-03-12 ((2456364j,0s,0n),+0s,2299161j)>
Related
This question already has an answer here:
In Ruby, why does nil[1]=1 evaluate to nil?
(1 answer)
Closed 6 years ago.
Since ruby 2.3.0, you can call []= method on nil. I don't understand the purpose of this method.
For instance:
nil[1] = 1
# or
nil['foo'] = 'bar'
but [] method does not exist:
nil[1]
# => NoMethodError: undefined method `[]' for nil:NilClass
The ruby 2.3.0 changelog does not mention that changes, although it seems close to the safe navigation operator.
What is the purpose of this operator?
That seems to be actually a bug in 2.3.0 - https://bugs.ruby-lang.org/issues/11976
It does not evaluate the arguments:
nil[undefined_index_variable] = raise "Fooo!" # => nil
That method isn't documented in Ruby 2.3.0 and I cannot reproduce this behavior in Ruby 2.3.1 (both examples raise NoMethodError: undefined method '[]=' for nil:NilClass).
Furthermore I reinstalled 2.3.0 and was only partly able to reproduce your examples:
$ rbenv install 2.3.0
Downloading ruby-2.3.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.bz2
Installing ruby-2.3.0...
Installed ruby-2.3.0 to /Users/spickermann/.rbenv/versions/2.3.0
$ rbenv shell 2.3.0
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
$ irb
irb > RUBY_VERSION
irb => "2.3.0"
irb > nil[1] = 1
irb => nil
irb > nil['foo'] = 'bar'
NoMethodError: undefined method `[]=' for nil:NilClass
from (irb):3
from /Users/spickermann/.rbenv/versions/2.3.0/bin/irb:11:in `<main>'
It seems like NilClass#[]= doesn't work properly in Ruby 2.3.0. Since it was completely removed in 2.3.1, I guess that this method or this behavior was added by accident.
Update: Cary Swoveland pointed out in a comment on another question that this behavior was a bug and was fixed in later versions (see: https://bugs.ruby-lang.org/issues/11976).
I got the following output from pry when trying out the example from http://rubini.us/doc/en/systems/concurrency/ with Rubinius 2.2.9:
2.1.0 (main):0 > RUBY_VERSION
=> "2.1.0"
2.1.0 (main):0 > RUBY_PATCHLEVEL
=> 0
2.1.0 (main):0 > RUBY_PLATFORM
=> "x86_64-darwin13.2.0"
2.1.0 (main):0 > RUBY_ENGINE
=> "rbx"
2.1.0 (main):0 > require 'actor'
LoadError: no such file to load -- actor
from kernel/common/code_loader.rb:441:in `load_error'
Is the documentation outdated?
It looks like the documentation is slightly outdated.
Commit 79bd4c30 appears to remove the Actor api from the Rubinius standard library and extract it into the rubinius-actor gem. As far as I can tell with a quick glance, it's the exact same API as the documentation describes.
I am using ruby version 2.0.0, and Using soap4r (1.5.8).
But i couldn't able to load require "soap/wsdlDriver".
I am trying to use with in rails console,
irb(main):001:0> require 'soap/rpc/driver'
=> false
irb(main):002:0> require 'soap/rpc/driver'
=> false
irb(main):003:0> require "soap/wsdlDriver"
=> false
The standard soap library was removed from Ruby. You will need to find an other solution. Perhaps this gem: https://rubygems.org/gems/soap4r-ruby1.9
Or you refactor you code to use a more up-to-date client. Find a list here: https://www.ruby-toolbox.com/categories/soap
I remember the words said by Matz: Ruby 1.8 will be dead soon. But I have no option. Here, I am using Ruby 1.8.7. Big Decimal is behaving differently compare to later version of Ruby.
For example:
ree-1.8.7-2011.12 :001 > require 'bigdecimal'
=> true
ree-1.8.7-2011.12 :002 > b=BigDecimal('0.0')
=> #<BigDecimal:9ce7148,'0.0',4(8)>
ree-1.8.7-2011.12 :003 > b
=> #<BigDecimal:9ce7148,'0.0',4(8)>
ree-1.8.7-2011.12 :004 > b==0
=> true
ree-1.8.7-2011.12 :005 > [b,b,0,0].uniq
=> [#<BigDecimal:9ce7148,'0.0',4(8)>, #<BigDecimal:9ce7148,'0.0',4(8)>] #Integer 0 is removed
ree-1.8.7-2011.12 :008 > [b,b,0,0].uniq.uniq
=> [#<BigDecimal:9ce7148,'0.0',4(8)>] #Applying two times uniq gives desired result for given array
Is there any patch to fix this issue? Sorry I don't have option to upgrade Ruby. Any help?
Thank you.
You might try https://github.com/marcandre/backports, which claims to provide "The latest features of Ruby backported to older versions". I didn't see anything in there regarding BigDecimal, at a glance, but who knows what it might be using internally. I've used it before for arrays, it's at least worth a shot.
Because apparently require 'date' doesn't include the method hours or seconds etc:
undefined method `hours' for 5:Fixnum (NoMethodError)
Am I missing something? Is 5.seconds only something you can do in Rails? If so, what is the require statement I need to get this to work in a ruby script?
Old question, but for the googlers like me:
require 'active_support/time'
For gem version 3.2.11, anyway.
The following works for me
irb
>> require 'active_support'
=> true
>> 5.hours
=> 18000 seconds
Depending on your environment and rails version you may need to require 'rubygems' this should be done before the require 'active_support' line.
You may also have to require 'activesupport' instead of active_support if you have an older version of rails.
ActiveSupport::CoreExtensions::Numeric::Time maybe