How to check if an instance variable is defined in a Ruby class

dog.rb


class Dog
  def initialize(name)
    @name = name
  end

  def set_nickname(nickname)   #set with ruby_I don't use hoge, but this time I'll give it a try for simplicity!
    @nickname = nickname
  end

  def print_nickname
    if !defined?(@nickname)
      puts('Not Set')
      return
    end
    puts(@nickname)
  end
end

If it is nil when you refer to if @age, you don't know which of the following cases.

-Is it nil because it has never been defined? --Is it the result of assigning @nickname = nil once?

In such a case, you should use defind? (@Age).

Recommended Posts

How to check if an instance variable is defined in a Ruby class
[Ruby] Does self refer to a class or an instance?
How to change a string in an array to a number in Ruby
[Ruby] I want to put an array in a variable. I want to convert to an array
If the parameter is an array, how to include it in Stopara's params.permit
How to start a subscript from an arbitrary number in Ruby iterative processing
How to install Ruby on an EC2 instance on AWS
How to launch another command in a Ruby program
[Ruby] What is an instance?
How to handle an instance
Library to check if it is an IPv4 format string
How to retrieve the hash value in an array in Ruby
How to display a graph in Ruby on Rails (LazyHighChart)
Mapping to a class with a value object in How to MyBatis
[Java] A class is an OS, and an instance is a virtual computer.
[Ruby] How to batch convert strings in an array to numbers
How to iterate infinitely in Ruby
How to install Bootstrap in Ruby
[Ruby] How to count even or odd numbers in an array
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
How to output the value when there is an array in the array
How to write ruby if in one line Summary by beginner
How to store the information entered in textarea in a variable in the method
How to create a web server on an EC2 instance on AWS
How to think about class design (division) in a business system (1)
I tried to make a parent class of a value object in Ruby
How to get date data in Ruby
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to insert a video in Rails
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
How to define an inner class bean
Notation to put a variable in a string
How to change the value of a variable at a breakpoint in intelliJ
How to publish a library in jCenter
[Ruby] How to prevent errors when nil is included in the operation
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
If it is Ruby, it is efficient to make it a method and stock the processing.
How to make a judgment method to search for an arbitrary character in an array
[Rails] If CSS is not applied in collection_select, check if class can be specified.
How to get keycloak credentials in interceptor class
How to check the logs in the Docker container
How to display a web page in Java
How to write an if statement to improve readability-java
How to create a class that inherits class information
How to get Class from Element in Java
How to convert a solidity contract to a Java contract class
How to run a djUnit task in Ant
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to conditionally add html.erb class in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails
[Ruby] How to generate a random alphabet string
How to solve an Expression Problem in Java
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
How to build the simplest blockchain in Ruby
How to build an executable jar in Maven
How to check Rails commands in the terminal