Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
クラスの宣言、インスタンス生成のサンプル
class User
attr_accessor :first_name, :last_name, :age
// コンストラクタ Constructor
def initialize(last_name, first_name, age)
self.last_name = last_name
self.first_name = first_name
self.age = age
end
def first_name
@first_name
end
def first_name=(string)
@first_mame = string
end
def name
last_name + ' ' + first_name
end
end
user1 = User.new('hoge', 'fuga', 39)
user2 = User.new('Tachibana', 'Yutaka', 30)
user3 = User.new('Nakatsukasa', 'Yuya', 28)
user4 = User.new('Suzuki', 'Takayuki', 28)
user5 = User.new('Teraji', 'Takashi', 32)
users = [user1, user2, user3, user4]
users.each do |user|
puts user.name
end
sum_age = 0
users.each do |user|
sum_age += user.age
end
puts sum_age / 5.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.