# Classes and Hashes

**URL:** <https://www.ruby-forum.com/t/classes-and-hashes/261184>\
**Category:** Development\
**Created:** [April 3, 2021, 8:00pm UTC](https://www.ruby-forum.com/t/classes-and-hashes/261184 "2021-04-03T20:00:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhj4](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/rhj4/32/2713_2.png) [@rhj4](https://www.ruby-forum.com/u/rhj4)\
**Post date:** [April 3, 2021, 8:00pm UTC](https://www.ruby-forum.com/t/classes-and-hashes/261184/1 "2021-04-03T20:00:51Z")

</div>

I have many years of programming experience, but I am a newbie to Ruby. I have started by creating a Contact class that either contains hashes such as Addresses, Phone Numbers, Email Addresses, etc. Alternatively, I am trying to Addresses and the other classes as inheriting from the Contact class.

What I am seeking is feedback on both approaches. For the Hashes in the Contact class, I am looking for a simple code example of initializing and populating the hash.

For using inheritance, what is the syntax to access the “Home Address” of a contact. Person.Address[“Home”}?

Thanks for any help you can provide

---

<div class="post-metadata">

**Author:** ![SouravGoswami](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/souravgoswami/32/2543_2.png) [@SouravGoswami](https://www.ruby-forum.com/u/SouravGoswami)\
**Post date:** [April 4, 2021, 7:43pm UTC](https://www.ruby-forum.com/t/classes-and-hashes/261184/2 "2021-04-04T19:43:19Z")

</div>

Hi, welcome! One approach could be this:

```auto
class Person
	def initialize(name:, address:, phone:)
		@name, @address, @phone = name, address, phone
	end

	def address
                # you can also use attr_reader(:address) instead of defining this method
		@address
	end
end

person = Person.new(name: 'Xman', address: {'Home' => 'The Earth'}, phone: '0003869723')

p person.address # => {:Home=>"The Earth"}
p person.address['Home'] # "The Earth"

```

The other one could be this!

```auto
class Person
	attr_reader(:address)

	def initialize(name:, home:, office: nil, phone: nil)
		@name = name
		@address = {'Home' => home, 'Office' => office}
		@phone = phone
	end
end

person = Person.new(name: 'Xman', home: 'Mars', office: 'Earth')

p person.address # => {"Home"=>"Mars", "Office"=>"Earth"}
p person.address['Home'] # "Mars"

```
