Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Built-in Ruby methods

Tags:

methods

ruby

If there is a defined built in ruby method, is it always preferable to use the built-in method?

If I have code such as:

if i == 0

What is the advantage to instead using the built-in ruby method?

if i.zero?
like image 897
tjhnc Avatar asked Jan 01 '14 16:01

tjhnc


1 Answers

i.zero? works only if i is Numeric object.

i = nil
i == 0
# => false
i.zero?
# NoMethodError: undefined method `zero?' for nil:NilClass
#        from (irb):5
#        from C:/Ruby200-x64/bin/irb:12:in `<main>'
like image 186
falsetru Avatar answered Sep 18 '22 12:09

falsetru