gouf
8/26/2014 - 6:14 PM

Person class

Person class

class Person
  attr_accessor :first_name, :last_name,
    :age, :hobbies, :developer
  attr_reader :gender
  def initialize(first_name, opts = {})
    @first_name = first_name
    @last_name, @age, @email, @hobbies, @gender =
      opts.values_at(:last_name, :age, :email, :hobbies, :gender)
  end

  def to_s
    <<-EOF.gsub(%r{^\s+\|}, '')
      |Name: #{name}
      |Age: #{@age}
      |Gender: #{@gender || 'undefined'}
      |Hobbies: #{@hobbies.join(', ')}
      |Developer?: #{@developer ? 'YES' : 'NO'}
    EOF
  end

  def name
    "#{@first_name} #{@last_name}"
  end

  def developer?
    @developer
  end
end

personal_data = {
  age: 25,
  last_name: 'Furuya',
  email: 'innocent.zero@gmail.com',
  hobbies: %w(read\ book listen\ music write\ ruby\ code),
  developer: false
}
gou = Person.new('Gou', personal_data)

p gou.name, gou.age #=> "gou furuya" 25
puts gou.to_s #=>
=begin
Name: Gou Furuya
Age: 25
Gender: undefined
Hobbies: read book, listen music, write ruby code
Developer?: NO
=end