1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

第十一回:クラス

Last updated at Posted at 2020-12-31

クラスの作成

クラスの初期化はinitializeで行う.名前を受け取った場合は「Hello name」と返し,受け取らなかった場合は「Hello world」と返すコードを作成するとすると下記のようになる.

class Hello
  def initialize
    name = gets_name
    puts_hello name
  end

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

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

Hello.new

ここで,nameを@nameにすると,以下のようにコードが変更される.@変数名で、クラス変数となる。

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
  end
end

Hello.new

参考サイト

参考サイトは以下の通り.


  • source ~/grad_members_20f/members/yuhsuzu/r11.org
1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?