In Ruby, Object is the root class of all objects. Although you can have your own Object class within User, it could cause a lot of confusion.
Let's simplify your problem by removing the User module (it's not relevant to the example) and by renaming Object to Foo (you'll find a better name). To initialize instance variables you can use the initialize method which is invoked by default every time you construct an object via new:
class Foo
def initialize
@data = {}
end
end
foo = Foo.new
#=> #<Foo:0x00007fb551823d78 @data={}>
# ^^^^^^^^
That hash you assign to @data will be shared among all instance methods. In addition, each Foo instance will have its own @data hash. To merge! another hash into it, you can use:
class Foo
def initialize
@data = {}
end
def add(hash)
@data.merge!(hash)
end
end
foo = Foo.new
#=> #<Foo:0x00007fbc80048230 @data={}>
foo.add({"abc"=>123})
#=> {"abc"=>123}
foo.add({"def"=>456})
#=> {"def"=>456}
foo
#=> #<Foo:0x00007fbc80048230 @data={"abc"=>123, "def"=>456}>
In order to chain multiple add calls (a so-called fluent interface), you have to return self from within the method:
class Foo
def initialize
@data = {}
end
def add(hash)
@data.merge!(hash)
self # <- like this
end
end
foo = Foo.new
#=> #<Foo:0x00007ff7408003d8 @data={}>
foo.add({"abc"=>123}).add({"def"=>456})
#=> #<Foo:0x00007ff7408003d8 @data={"abc"=>123, "def"=>456}>
Finally, to add static data, you could simply call your own method:
class Foo
def initialize
@data = {}
end
def add(hash)
@data.merge!(hash)
self
end
def add_more
add({"more" => 789})
end
end
foo = Foo.new
#=> #<Foo:0x00007f99b20f8590 @data={}>
foo.add({"abc"=>123}).add({"def"=>456}).add_more
#=> #<Foo:0x00007f99b20f8590 @data={"abc"=>123, "def"=>456, "more"=>789}>
class << selfrabbit hole ;-)class << selfin the snippet above first of all? It is doing some "magic" you better should not touch until you are comfortable with some basics (it's not magic at all, but it does require a bit deeper understanding of Ruby object model). Create a simple class that does the necessary job, then move further if necessary...Objectis an existing class, maybe you can come up with another name for your class.