How to Check Nil Object in Ruby

Ruby allows the programmer to access data using named variables that point to a location in memory. Normally, this works fine, but sometimes an error or other event can cause the variable to point to "nil," a special data type that lets the programmer know that the variable does not point to any data. Programmers can check for nil and react to it appropriately using a special method that all variables inherit.

Instructions

    • 1

      Open a terminal. In Windows, click "Start," "Run" and type "cmd." In Mac OS X, click the Spotlight icon and type "Terminal."

    • 2

      Type "irb" into the terminal to open the interactive Ruby interpreter.

    • 3

      Type the following into the terminal to define a variable as nil in Ruby:

      d = nil

    • 4

      Type the following to check if a variable is nil:

      d.nil?

      The result will be either "true" or "false."

Tips & Warnings

  • "nil?" will check if a variable is nil, but will still generate an error if it is run on a variable that was never defined at all. In order to check if a variable is defined, you must use the "defined?" operator. Type the following to do so: "defined? d".

Related Searches:

References

Comments

You May Also Like

  • How to Create a Class in Ruby

    Ruby is a language built for defining classes, as it is first and foremost an object-oriented language. Ruby provides a number of...

  • How to Use Inheritance in Ruby

    Inheritance is the core of object oriented programming. Building class structures makes smaller programs easy, and large programs possible. In Ruby, inheritance...

  • How to Create a While Loop in Ruby

    When you create a while loop in Ruby, you're essentially saying "while X is true, do Y" or even "until X is...

  • How to Uninstall a Ruby Gem

    Features can be added to the Ruby programming language by downloading and installing special packages called gems. These packages are fetched from...

  • How to Compare Strings in Ruby

    Ruby is an extremely powerful programming/scripting language. With powerful capabilities and an efficient syntax, many things are possible with Ruby. This article...

  • How to Create a Multidimensional Array in Ruby

    Though Ruby doesn't provide explicit support for multidimensional arrays, you can implement one yourself if you have a basic knowledge of the...

  • SQL Date Conflict Check

    The Structured Query Language (SQL) is used to add, modify and retrieve (query) data from a database. The SQL language includes a...

  • How to Convert a Date to a String in JavaScript

    JavaScript, like Java, assigns each piece of data a class. Dates are stored in a compressed format to save memory. Data classes...

  • How to Play Spades Nil or Blind Nil

    Check to see the quality of your hand. In spades, high cards of each suit tend to win tricks, and spades trump...

  • How to Compare Java Script String

    String comparisons in JavaScript are commonly used for form validation, parsing text and responding to user input. In JavaScript, comparing strings is...

  • Value of Rubies Over Time

    Rubies have consistently been the most valued gemstone in the world. Over time, the ruby has been considered as more valuable than...

  • How to Create an Array in Ruby

    There are three primary ways to create an array in Ruby: the array literal, building the array and returning an array. An...

Related Ads

Featured