Of course, some times you can use objects and hashes for the same thing. Storing key value pair ob objects like this:
[3] pry(main)> class Person
def initialize(name, gender)
@name = name
@gender = gender
end
end
[3] pry(main)* => :initialize
[4] pry(main)> x = Person.new("Dan", "M")
=> #<Person:0x00000003708098 @gender="M", @name="Dan">
[13] pry(main)> y = Person.new("Peter", "M")
=> #<Person:0x0000000391fca0 @gender="M", @name="Peter">
[22] pry(main)> z = {name: "Maria", gender: "F"}
=> {:name=>"Maria", :gender=>"F"}
But this objects really doesn't get all the power of an object oriente programming language from the definitions of an class/object and hash:
Ruby is a perfect Object Oriented Programming Language. The features
of the object-oriented programming language include:
Data Encapsulation:
Data Abstraction:
Polymorphism:
Inheritance:
These features have been discussed in Object Oriented Ruby.
An object-oriented program involves classes and objects. A class is
the blueprint from which individual objects are created. In
object-oriented terms, we say that your bicycle is an instance of the
class of objects known as bicycles.
Take the example of any vehicle. It comprises wheels, horsepower, and
fuel or gas tank capacity. These characteristics form the data members
of the class Vehicle. You can differentiate one vehicle from the other
with the help of these characteristics.
A vehicle can also have certain functions, such as halting, driving,
and speeding. Even these functions form the data members of the class
Vehicle. You can, therefore, define a class as a combination of
characteristics and functions.
and a hash:
A Hash is a collection of key-value pairs like this: "employee" =>
"salary". It is similar to an Array, except that indexing is done via
arbitrary keys of any object type, not an integer index.
So for store data I recommend you a Hash.
On the other hand, as showed in a comment the number that appers in the object representation is the object id, but with few operations added:
1) bitwise left shift:
5 << 1 # gives 10
2) passed to hexadeimal
(10).to_s(16)
"a"
pry(main)> x = Person.new("Dan", "M")
=> #<Person:0x00000003708098 @gender="M", @name="Dan">
[5] pry(main)> x.object_id
=> 28852300
[8] pry(main)> (x.object_id << 1 ).to_s(16)
=> "3708098"
finally in ruby you can get the hash representation of an object like this:
x.instance_variables.each {|var| hash[var.to_s.delete("@")] = x.instance_variable_get(var) }
0x00...is(object_id << 1).to_s(16)(object_id, shifted left by one in 16 numeral base.) About what the object is in OOP there are tons of books written. You should not expect to receive an answer on SO for this sort of questions, for that people study years and read many books.Hash, and instance variable names and values be stored as key-value pairs in them. In fact, JavaScript works somewhat like that.