0
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 5 years have passed since last update.

ruby クラス 自分用 メモ

Posted at

'''
def sayHi(name = "tom") # 引数
score = 80

puts "hi! #{name}"

return "hi! #{name}"
end

sayHi("taguchi")

sayHi "taguchi"

sayHi

p sayHi

p score
'''

⇒クラス内で定義した変数には、外部からアクセス出来ない
@scoreなら、同じインスタンス(同じエリア内で)内で可能
⇒ attr_accessor :name

setter: name=(value)

getter: name

■クラス
主語
自分で作ることもできる

class User
end

■メソッド
述語
自分で作ることもできる

def sayHi!
 puts "Hi!"
end

■self
メソッドを受け取っているインスタンス自身
⇒何にでもなる

■クラスメソッド
def self.info
puts "User Class"
end

User.info
⇒技の省略、組み合わせが可能

■クラス変数
⇒よくわからん

わかりやすい
Rubyにおけるスコープのおはなし

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