Class in Ruby


Preface (Introduction)

As a basic idea of ​​object orientation,

--Capsulation --Inheritance --Polymorphism

there is.

This idea is incorporated not only in Ruby but also in Python and various languages.


If you enter a name in the argument of the command created earlier, a greeting will be returned.


def hello name
  puts "Hello #{name}."

name = ARGV[0]

We will improve this and further classify it.

Get the name

In hello \ _method.rb, I tried to return the command argument, but with this, if nothing is passed,

> ruby hello_method.rb
-> Hello .

Only a greeting is returned.

It's lonely as it is, so when you prepare the gets \ _name method and do not pass anything to the command argument

Hello world.

I will return.


def hello name
  puts "Hello #{name}."

def gets_name
  name = ARGV[0] || 'world'
  return name

name = gets_name
hello name


Class this code.


class Greeter
  def initialize
    @name = gets_name

  def puts_hello #salute
    puts "Hello #{@name}."

  def gets_name
    name = ARGV[0] || 'world'
    return name.capitalize

When you do this

> ruby class_greeter.rb
-> Hello World.

> ruby class_greeter.rb W
-> Hello W.

You can have something that returns the same output as before classifying.


--class Greeter: Greeter class definition --def initialize: Function to initialize. Does it work like a constructor such as C ++? -@name: Member variable Create an instance of the Greeter class

Is the basis for classification in Ruby.

Reference material

Chart type ruby-VI (hello class)

Recommended Posts

Class in Ruby
Write class inheritance in Ruby
Heavy in Ruby! ??
About eval in Ruby
Output triangle in Ruby
Variable type in ruby
Fast popcount in Ruby
NullpointException in injecting class
Recommendation of Service class in Ruby on Rails
ABC177 --solving E in Ruby
Validate JWT token in Ruby
Implemented XPath 1.0 parser in Ruby
Read design patterns in Ruby
[Self-use memo] (Ruby) class, class instance, instance
Integer unified into Integer in Ruby 2.4
Use ruby variables in javascript.
Multiplication in a Ruby array
About regular expressions in Ruby
[Ruby / Rails] Set a unique (unique) value in the class
What are Ruby class methods?
Birthday attack calculation in Ruby
Judgment of fractions in Ruby
Find Roman numerals in Ruby
Try using gRPC in Ruby
[Ruby] Class methods, instance methods, etc.
[Ruby] Find numbers in arrays
NCk mod p in Ruby
Chinese Remainder Theorem in Ruby
[Swift] Create UIButton class in code
Sorting hashes in a Ruby array
Basics of sending Gmail in Ruby
How to iterate infinitely in Ruby
Achieve 3-digit delimited display in Ruby
Encoding when getting in Windows + Ruby
Run GraphQL Ruby resolver in parallel
Ruby on Rails Japanese-English support i18n
[Ruby] Extracting double hash in array
[Ruby] then keyword and case in
How to install Bootstrap in Ruby
String output method memo in Ruby
Implement a gRPC client in Ruby
StringBuffer and StringBuilder Class in Java
Write keys and values in Ruby
[Ruby] Date, Time, Datetime class basics
[Super Introduction] About Symbols in Ruby
Hanachan in Ruby (non-destructive array manipulation)
Manipulating data in GCS during Ruby
Is there no type in Ruby?
Try file locking in Ruby directory
[Ruby] undefined method `dark?'occurs in rqr_code
openssl version information in ruby OPENSSL_VERSION
Ruby methods often used in Rails
Returns any Map class in Collectors.toMap
Make Ruby segfault in two lines
Ruby learning 4
What is a class in Java language (3 /?)
I tried a calendar problem in Ruby
Implementing poker little by little in Ruby Part 2
[Ruby] Array
How to get date data in Ruby
Ruby basics