LoginSignup
5
0

More than 3 years have passed since last update.

ruby-VI(hello class)

Last updated at Posted at 2020-11-25

学習内容

  • class,object

クラス、オブジェクト

  • class

    • クラスとは: 一種のデータ型、「オブジェクト設計図」や「オブジェクトの雛形」とも呼ばれることもある。
    • クラスが同じならば保持している属性や使えるメソッドは(原則として)同じになる。
  • object

    • クラスをもとにして作られたデータのかたまり
    • 同じクラスから作られたオブジェクトは同じ属性、メソッドを持つが、属性に保持されるデータは異なる。

code

require 'colorize'

# method
def hello(name)
  "Hello #{name}."
end

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

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

# method call
name = ARGV[0]
puts "method call"
puts hello(name).green

# inherited class call
name = Hello.new(ARGV[0])
puts "inherited class call"
puts name.hello.green

# extend class call, override
puts "extend class call, override"
puts ARGV[0].hello.green

参考

講義ページ

プロを目指す人のための Ruby 入門


  • source ~/classes/muli_scale/grad_members_20f/members/keita_k7/memo/c10.org
5
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0