11th, Classification with Ruby

!Mac OS-11.0.1!ruby-2.6.6p146

Class

** This article is about the lecture [Special Lecture on Multiscale Simulation] in the first half of graduate school **
We will deal with Class in Ruby.

Hello method before classifying

def hello(name)
  puts "hello #{name}"
end

def gets_name
  name = ARGV[0]
  if name == nil
    puts "What\'s your name? "
    name = gets.chomp
  end
  return name
end

hello gets_name

On the other hand, let's define Hello Class and look at Class in Ruby.

Suddenly Class

Suddenly, take a look at the source code that has been Classified.

class Hello
  def initialize
    @name = gets_name
    puts_hello
  end

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

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

Hello.new

So that's it. Easy. The points to note here are described below.

Inheritance or override

Learn about this by looking at the code.

#Inheritance
class Hello < String
  def hello
    "Hello #{self}."
  end
end

#override
class String
  def hello
    "Hello #{self}."
  end
end

So that's it. Very simple. Here again, Ruby is extremely easy and simple, and the learning cost is low.
By the way, calling and using are as follows

require 'colorize'
#Inheritance call
greeter = Hello.new(ARGV[0])
puts greeter.hello.green

#Override call
puts ARGV[0].hello.green

Ruby easy. I'm doing embedded development in C, but I don't want to. that's all.


Recommended Posts

11th, Classification with Ruby
Install Ruby 3.0.0 with asdf
Getting Started with Ruby
Evolve Eevee with Ruby
Creating a Cee-lo game with Ruby 4th Creating a game progress process
Solve Google problems with Ruby
I tried DI with Ruby
GraphQL Client starting with Ruby
Ruby: Send email with Starttls
Leap year judgment with Ruby
Format Ruby with VS Code
Integer check method with ruby
Ruby Learning # 8 Working With String
[Ruby] problem with if statement
Studying with CodeWar (ruby) ⑤ proc
Use Ruby with Google Colab
[Ruby] REPL-driven development with pry
Getting Started with Ruby Modules
[ruby] Method call with argument
Install Ruby on MSYS2 with pacman
Make electronic music with randomness with Ruby
Ruby Scraping-Move Selenium Headless with VPS.
5th
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
6th
6th
Let's use Amazon Textract with Ruby
Programming with ruby (on the way)
Studying with CodeWar (ruby) ④ case ~ when
9th
[Ruby] Extracting elements with slice method
7th
[Ruby] Handle instance variables with instance methods
11th
9th
7th
Impressions of making BlackJack-cli with Ruby
11th
10th
10th
Install ruby on Ubuntu 20.04 with rbenv
Make a typing game with ruby
Solve with Ruby AtCoder ABC177 D UnionFind
Make Ruby Kurosawa with Ruby (Ruby + LINE Messaging API)
Send an email from gmail with Ruby
[Ruby on Rails] View test with RSpec
Introduction to Ruby basic grammar with yakiniku
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Let's make draw poker with ruby ~ Preparation ~
Ruby / Rust linkage (3) Numerical calculation with FFI
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
[Ruby] 4-digit random number generation with sprintf
Let's make a smart home with Ruby!
I made a risky die with Ruby
Extract a part of a string with Ruby
Authenticate child accounts with Stripe Connect | Ruby
[Ruby] Classification and usage of loops in Ruby
Ruby / Rust linkage (4) Numerical calculation with Rutie
[At Coder] Introducing Competitive Pro with Ruby
Convert JSON to TSV and TSV to JSON with Ruby