About Ruby classes and instances

Programming study diary

July 11, 2020 Progate Lv.175 RubyⅣ

Classes and instances

Classes and instances are briefly described here. In order to create a "thing" called a menu by programming, the blueprint is first required. A blueprint is called a class, and a "thing" is called an instance. (Create a "thing" (instance) based on the blueprint (class)), so to create an instance, prepare a class, create an instance from the class, and add information to the instance.

Class definition

Classes can be defined with class class name. Be sure to start the class with uppercase letters and remember to write ʻend`. </ font>

index.rb


class Menu
end

attr_accessor An instance variable that can be directly modified and manipulated. Write ʻattr_accessor symbol` to have information. You can also use multiple instance variables for a class.

index.rb


class Menu
  attr_accessor :name
end

Instance generation

To create a new instance based on the class, use class name.new. You can assign the generated instance variable by setting variable name = class name.new. In order to give information to the instance, assign a value to the instance variable prepared by the class. You can set a value in an instance variable with instance.variable name = value.

index.rb


class Menu
  #processing
end
#Create an instance from the Menu class and assign it to the variable menu1
menu1=Menu.new
menu1.name="Sushi"

Use methods in class

The method defined in the class is called as it is used for the instance. Call with instance.method name. A method that calls a method defined in a class is called an instance method.

index.rb


class Menu
  attr_accessor :name
  attr_accessor :price
  #Define methods in class
  def show
    puts "menu"
  end
end
menu1=Menu.new
#Calling a method defined in a class
menu1.show

console


menu

Instance method

A method to call for an instance defined in a class. It can accept arguments and return a return value. The "instance variable" that is the information that the instance has and the instance method that is called for the instance are defined in the class.

index.rb


class Menu
  #processing
  def show(data)
    return "#{data}is"
  end
end
menu1=Menu.new
puts menu1.show("menu")

console


It is a menu

Use instance variables in instance methods

Instance variables can be handled by using a special variable self in the instance method and making it self.variable name. In the instance method, the called instance itself is assigned to the variable self.

index.rb


class Menu
  #processing
  def show_name
    puts "#{self.name}is"
  end
end
menu1=Menu1.new
menu1.name="Sushi"
#Instance method call
menu1.show_name

console


It's sushi

initialize method

Processing can be executed immediately after creating an instance. Called automatically immediately after creating an instance with class name.new.

index.rb


class Menu
  #processing
  def initialize
    puts "menu"
  end
end
#Menu.Automatically call initialize method when new is executed
menu1=Menu.new

console


menu

In the instance method, you can handle the instance variable with self.variable name, and assign the value to the instance variable with self.variable name = value.

index.rb


class Menu
  #processing
  def initialize
    self.name="Sushi"
  end
end
menu1=Menu.new
puts menu1.name

console


Sushi

Variable of initialize method

You can also pass arguments to the initialize method. You can pass the value by passing an argument to class.new. Of course, keyword arguments can also be used.

index.rb


class Menu
  #processing
  def initialize(message)
    puts message
  end
end
menu1=Menu.new("Hello")

console


Hello

require Read the file. (Used when the main statement is separated and you want to read a class in another file)

index.rb


requie "./menu"
menu1=Menu.new(name: "Sushi"

menu.rb


class Menu
  attr_accessor :name
  attr_accessor :price
end

gets.chomp Used when accepting input. When this code is executed, the console will be in a state of waiting for input. By setting variable = gets.chomp, the input value can be assigned to the variable until the enter key is pressed.

index.rb


puts "Enter name"
name=gets.chomp
puts "What's your name#{name}is"

console


Enter name
Tanaka (Enter)
Your name is Tanaka

If you want to receive a number, you can convert the input to a number by doing gets.chomp.to_i.

index.rb


puts "Enter numbers"
number=gets.chomp.to_i #Convert the entered content to a number
puts "#{number}Twice#{number*2}is"

Recommended Posts

About Ruby classes and instances
[Ruby] Classes and instances
About classes and instances
Ruby classes and instances
About classes and instances (evolution)
Consideration about classes and instances
Creating Ruby classes and instances
Classes and instances
About the difference between classes and instances in Ruby
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Write code using Ruby classes and instances
java (classes and instances)
Getting Started with Java_Chapter 8_About Instances and Classes
About Ruby hashes and symbols
About Ruby and object model
[Ruby] Singular methods and singular classes
Ruby methods and classes (basic)
[Ruby] Singular methods and singular classes
About Ruby single quotes and double quotes
[Ruby] Classes, instance variables, instances, etc ...
Organize classes, instances, and instance variables
About Ruby product operator (&) and sum operator (|)
About object-oriented inheritance and about yield Ruby
Classes and instances Java for beginners
[Ruby] Creating code using the concept of classes and instances
About Ruby symbols
About ruby ​​form
Java programming (classes and instances, main methods)
About standard classes
About Ruby arrays
About Ruby inheritance
About Ruby Hashes
About Ruby Symbols
Comparison of JavaScript objects and Ruby classes
Ruby and Gem
About Ruby methods
[Ruby] About variable naming rules. Also naming conventions for constants, classes, and methods.
About Ruby Kernel Module
About Ruby error messages
Symbols and Destructive Ruby
HashMap and HashSet classes
About Ruby Hashes (continued)
About eval in Ruby
String literals and instances
About Bean and DI
[ruby] About here documents
About Ruby if statement
[Java] About anonymous classes
About gets and gets.chomp
About redirect and forward
Ruby inheritance and delegation
About encapsulation and inheritance
Ruby variables and methods
[Ruby] About instance generation
List and happy classes
About Serializable and serialVersionUID
About the [ruby] operator
Thinking about logic Ruby
[Ruby] I thought about the difference between each_with_index and each.with_index
A rough note about Ruby arrays and hash objects
[Technical memo] About the advantages and disadvantages of Ruby