Skip to content

Instantly share code, notes, and snippets.

@Jpsern
Created October 22, 2019 16:10
Show Gist options
  • Save Jpsern/c2c1a340e58fe7e53df5f8ff88261974 to your computer and use it in GitHub Desktop.
Save Jpsern/c2c1a340e58fe7e53df5f8ff88261974 to your computer and use it in GitHub Desktop.
クラス作成
=begin
https://qiita.com/kuboharu/items/d0e8d7e12bbe18f54729 回答例
問題.
「User.new(first_name: '太郎').first_name」で「太郎」を取得できるよう、
クラス「User」を作成してください。
また、「User.new(first_name: '太郎', last_name: '田中').full_name」で
「田中太郎」が取得できるよう、インスタンスメソッド「full_name」を定義してください。
=end
class User
attr_accessor :first_name, :last_name
def initialize(hash)
@first_name = hash[:first_name]
@last_name = hash[:last_name]
end
def full_name
@last_name + @first_name
end
end
p User.new(first_name: '太郎').first_name
p User.new(first_name: '太郎', last_name: '田中').full_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment