Hi,

This is my first post on this mailing list and I would like to thanks the
authors and the community for this great tools.

After a couple of weeks happily playing with Rails, I got bitten by a
strange behaviour drift between Rails and Ruby:

Plain Ruby:

$ irb
irb(main):001:0> 2 / 5
=> 2/5
irb(main):003:0> 2.to_f / 5.to_f
=> 0.4

Ruby On Rails:

$ cd /tmp
$ rails new t1
$ cd t1
$ rails console
Loading development environment (Rails 3.2.9)
irb(main):001:0> 2 / 5
=> 0
irb(main):002:0> 2.to_f / 5.to_f
=> 0.4

So I just don't get why 2/5 doesn't return the same result in both cases.
Personally, I found this behaviour dangerous since many third party
libraries will be based on it. I had a quick look at
http://guides.rubyonrails.org/active_support_core_extensions.html but could
not find any mention of it.

Can someone explain me why and how Rails does that?

Best regards,

--
Nicolas Desprès

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Nicolas Desprès at Dec 7, 2012 at 11:42 am
    I forgot to say that I am using ruby 1.9.3-p327.

    On Thu, Dec 6, 2012 at 9:41 AM, Nicolas Desprès
    wrote:
    Hi,

    This is my first post on this mailing list and I would like to thanks the
    authors and the community for this great tools.

    After a couple of weeks happily playing with Rails, I got bitten by a
    strange behaviour drift between Rails and Ruby:

    Plain Ruby:

    $ irb
    irb(main):001:0> 2 / 5
    => 2/5
    irb(main):003:0> 2.to_f / 5.to_f
    => 0.4

    Ruby On Rails:

    $ cd /tmp
    $ rails new t1
    $ cd t1
    $ rails console
    Loading development environment (Rails 3.2.9)
    irb(main):001:0> 2 / 5
    => 0
    irb(main):002:0> 2.to_f / 5.to_f
    => 0.4

    So I just don't get why 2/5 doesn't return the same result in both cases.
    Personally, I found this behaviour dangerous since many third party
    libraries will be based on it. I had a quick look at
    http://guides.rubyonrails.org/active_support_core_extensions.html but
    could not find any mention of it.

    Can someone explain me why and how Rails does that?

    Best regards,

    --
    Nicolas Desprès

    --
    Nicolas Desprès

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Frederick Cheung at Dec 7, 2012 at 11:47 am

    On Thursday, December 6, 2012 8:41:20 AM UTC, Nicolas Desprès wrote:
    Hi,

    This is my first post on this mailing list and I would like to thanks the
    authors and the community for this great tools.

    After a couple of weeks happily playing with Rails, I got bitten by a
    strange behaviour drift between Rails and Ruby:

    Plain Ruby:

    $ irb
    irb(main):001:0> 2 / 5
    => 2/5
    irb(main):003:0> 2.to_f / 5.to_f
    => 0.4
    That's not how it behaves for me. I get 2 / 5 == 0 in both cases. The ruby
    mathn library (part of the stdlib) does do this (and I really wish it
    wouldn't). Perhaps something in your irbrc pulls this in ?

    Fred

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/CQtn4nVU-OsJ.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nicolas Desprès at Dec 7, 2012 at 2:05 pm
    On Fri, Dec 7, 2012 at 12:46 PM, Frederick Cheung wrote:
    On Thursday, December 6, 2012 8:41:20 AM UTC, Nicolas Desprès wrote:

    Hi,

    This is my first post on this mailing list and I would like to thanks the
    authors and the community for this great tools.

    After a couple of weeks happily playing with Rails, I got bitten by a
    strange behaviour drift between Rails and Ruby:

    Plain Ruby:

    $ irb
    irb(main):001:0> 2 / 5
    => 2/5
    irb(main):003:0> 2.to_f / 5.to_f
    => 0.4
    That's not how it behaves for me. I get 2 / 5 == 0 in both cases. The ruby
    mathn library (part of the stdlib) does do this (and I really wish it
    wouldn't). Perhaps something in your irbrc pulls this in ?
    Not .irbrc. I forgot I had an alias on irb which pull -m option... :-( So
    the error was from my environment. Thanks for your reply.

    Sorry for the noise.

    --
    Nicolas Desprès

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 7, '12 at 11:42a
activeDec 7, '12 at 2:05p
posts4
users2
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase