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.
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, 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.
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