Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Chris Ward
Chris Ward
12,129 Points

I found the is_nil? method to be inadequate, so I replaced it with this

The is_nil? method has its place, but coming from other programming languages where there is a broader range of functionality associated with the nil/null/0 value, I wrote a more helpful method called is_conv_to_nil? (which stands for, is convertible to nil). In the next thread, I will show you how I use this function to overcome one of the problems I don't like with Ruby.

So, here is the is_conv_to_nil? method...Enjoy!

def is_conv_to_nil?(obj)
    if (obj == false || obj.class == NilClass) 
        return true
    end
    if (obj.is_a?(Numeric))
        begin
            return (obj.to_f == 0.0 && obj.to_i == 0)
        rescue
            return false
        end
    end
    if (obj.is_a?(String))
        return (obj.to_s == "")
    end
    begin
        return (obj.count == 0)
    rescue
        return false
    end
end