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.
Related
I'm writing a gem that I want to work across multiple Ruby versions, what's the best way to do this? The naive solution is to do stuff like this
if RUBY_VERSION <= 1.8.7
my_hash = {:a => 1}
elsif RUBY_VERSION >= 1.9.3
my_hash = {a: 1}
...
end
What's the best way to make your gem support multiple Ruby versions?
Ruby > 1.9.3 still supports the old hash syntax. If you need to support 1.8.7 and your only problem are hash literals, the elegant solution is to use the old syntax exclusively. This way you can drop any conditionals.
You can write two versions of gems on different files within the lib directory, and on the main file, load either of them depending on the Ruby version.
Main file (foo_gem/lib/foo.rb)
if RUBY_VERSION <= 1.8.7
require_relative "./foo-ruby1.8.7"
elsif RUBY_VERSION >= 1.9.3
require_relative "./foo-ruby1.9.3"
end
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'm mucking about with the Fog gem and have figured out how to get started:
1.9.3-p545 :008 > c = Fog::Compute::Ecloud.new({
1.9.3-p545 :009 > :base_path => '/cloudapi/spec',
1.9.3-p545 :010 > ecloud_authentication_method: 'basic_auth',
1.9.3-p545 :011 > ecloud_username: 'user#terremark.com',
1.9.3-p545 :012 > ecloud_password: 'password'
1.9.3-p545 :013?> })
[fog][WARNING] Unrecognized arguments: base_path
=> #<Fog::Compute::Ecloud::Real:25681720 #base_path="/cloudapi/spec" #connections={} #connection_options={} #host="https://services.enterprisecloud.terremark.com" #persistent=false #version="2013-06-01" #authentication_method=:basic_auth #access_key=nil #private_key=nil #username="user#terremark.com" #password="password">
I don't know what to do after this, though. How do I get the object to do anything useful? I'm new to Ruby so a lot of the code in the Fog Ecloud source doesn't make sense to me.
I've tried using different methods, but each tends to result in an error.
Can someone provide an example and explanation indicating where I need to go from here?
It looks like you have found a bug!
Fog is giving you this error because base_url is not present in the recognizes line.
I went ahead and fixed it for you. If you are using bundler you can use the latest master by updating your Gemfile to include the following
gem 'fog', :git => 'https://github.com/fog/fog.git'
Or alternatively you can just patch it in your code by executing the following code prior to using fog
require 'fog'
module Fog
module Compute
class Ecloud < Fog::Service
recognizes :ecloud_username, :ecloud_password, :ecloud_version,
:ecloud_access_key, :ecloud_private_key,
:ecloud_authentication_method, :base_path
end
end
end
For information about how to use fog, I would recommend reading the following page.
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)>
I thought Ruby automatically converts to Bignum. I found confirmation here
However, it is not happening:
ruby 1.8.7 (358) [universal-darwin12.0]
>> 2 ** 62
=> 4611686018427387904
>> 2 ** 63
=> -9223372036854775808 #why minus - how about automatic Bignum conversion?
>> 2 ** 64
=> 0 #- how about automatic Bignum conversion?
Use a Newer Ruby Version
Ruby 1.8.7 is (in Internet terms) ancient. Use something more recent. For example:
[1] pry(main)> RUBY_VERSION
=> "2.0.0"
[2] pry(main)> 2 ** 63
=> 9223372036854775808
[3] pry(main)> 2 ** 64
=> 18446744073709551616
That is probably a bug in the old version of Ruby. Switch to a newer version, the problem is gone. Today is the release day for Ruby 2.0. Ruby 1.8 will be dead soon. On my Ruby 1.9.3, I just did 2**1000000 without any problem except that it goes on for a while, so I had to terminate it.
Most likely a bug specific to the build you're using. For example, when I do ruby -v I get:
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin12.2.1], MBARI 0x6770, Ruby Enterprise Edition 2011.03
...and in an irb session I get:
1.8.7 :006 > 2 ** 64
=> 18446744073709551616
1.8.7 :007 > (2 ** 64).class
=> Bignum
1.8.7 :008 > RUBY_VERSION
=> "1.8.7"
I also don't get this problem if I use newer versions. If you can post your output from ruby -v that would shine some light on the situation. For example Ruby REE vs. MRI vs. JRuby, etc.
Also, and this is just an opinion so take it for what it's worth, but I don't think Apple is very good about keeping their built-in version of Ruby updated, so just in case you're using the built-in version then consider moving to another build.