to_f vs to_d in Ruby

My girlfriend asked me about the difference between to_f (to float) and to_d (to decimal) in Ruby. By the way, the method to_d is a support method from the ruby library “bigdecimal/util”

I figured that the bigdecimal library provides a more precise decimal number than floats. But it was not all.

After some exploration she explained to me that while it is true that to_d is more precise the main difference from to_f is that it does not work with nil.

require 'bigdecimal'
require 'bigdecimal/util'

nil = Nil.new

nil.to_f #=> 0.0

nil.to_d # => NoMethodError: undefined method `to_d' for nil:NilClass

12.to_d # 12.0

She continues explaining to me that this main reason why she needs to use float not decimal in her code (for example for when a user has not provided a number).

I love learning from her 🙂

Advertisements

11 thoughts on “to_f vs to_d in Ruby

  1. Good Day,

    Thank you for sharing the coding information.

    I was wondering if you might be able to advise on a coding solution i need on my website? Please let me know.

    Regards,

    Kirsten Slater.

      1. Awesome, thanks!

        Well what I’m looking for is to automate a ranking system of users, based on how frequently that user’s name appears on a certain page of my site (as they contribute to the site). So first I need a crawl to count the frequency of each users name on this page, and then hash it as an array or a similar solve, where the most frequently occurring user name is max (ranked 1) and least occurring user name is min.

        Is this something straight forward to code? Please advise, much appreciated.

        Regards,

        Kirsten.

      2. Yep that sounds straightforward. Are you learning coding at the moment? I can help you with the web scraping of your page. It is fun actually and I encourage you to learn it as well. What page is that?

  2. Thanks so much, I was doing self-taught coding at one stage but I didn’t practice enough to keep my skills up.

    So basically I run a crowd funding website, where users fund other users projects.

    What I’d like, is for those who fund projects to be ranked according to how many projects they have contributed toward financially (and not ranked on the dollar amount, but just how many projects).

    So if you invest $1 in 8 projects, and I invest $200 in 5 projects, you will still be ranked number #1 on the leaderboard and I will be #2 because you have helped more people than me. So basically it’s the funding page only that I want the frequency to count.

  3. Thanks. So the page that has all the projects is as follows:

    vindexfunding.com/all-funding-projects

    If you go into one of the projects for example, there will be a ‘backer’ tab to see all the names of those who invested in that project:

    vindexfunding.com/project/marketing-text-book/

    So I want all the backers across all projects to be ranked. Based on how many projects they have contributed to. I want the max to min frequency of user names to then be ranked on the homepage:

    vindexfunding.com

    Thanks again for checking it out 🙂

    1. Hey, I just got your message on my site. Yea i know it’s not the best, but i’m not a coder so i did what i could with what i had. I found a way to parse the site though, which seems to be working ok 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s