If you are receiving a hash as the sole argument, why not just keep that as an instance variable? Whenever you need a value, call it from the hash. You can keep the instance variable name short so that it can be easily called.
class Foo
attr_reader :p
def initalize p
@p = p
end
def foo
do_something_with(@p[:name])
...
end
end
If @p[:name]
is still too lengthy for you, then you can save a proc as an instance variable, and call the relevant value like @p.(:name)
.
class Foo
attr_reader :p
def initialize p
@p = ->x{p[x]}
end
def foo
do_something_with(@p.(:name))
...
end
end
Or, still an alternative way is to define a method that calls the hash and applies the key.
class Foo
def initalize p
@p = p
end
def get key
@p[key]
end
def foo
do_something_with(get(:name))
...
end
end
If want to set the values, you can define a setter method, and further check for invalid keys if you want.
class Foo
Keys = [:name, :age, :email, :gender, :height]
def initalize p
raise "Invalid key in argument" unless (p.keys - Keys).empty?
@p = p
end
def set key, value
raise "Invalid key" unless Keys.key?(key)
@p[key] = value
end
def get key
@p[key]
end
def foo
do_something_with(get(:name))
...
end
end