Abbrev
require 'abbrev'
class Apa
def initialize
@hsh = { parameter: 11, subject: 'Kalle Kula', parm: [:ost] }
@keys = @hsh.keys.abbrev
end
def method_missing(key)
@hsh[@keys[key.to_s]] || @hsh[@keys[key]]
end
end
apa = Apa.new
p apa.parameter # => "11"
p apa.parm # => [:ost]
p apa.param # => "11"
p apa.par # => << NO KEY
p apa.p # => << NO KEY
p apa.subj # => "Kalle Kula"
p apa.su # => "Kalle Kula"
p apa.sub # => "Kalle Kula"
The class holds a Hash with three keys: parameter
, subject
and parm
.
You write a getter:
def method_missing(name) = (@hsh[name])
But that would require full name: subject
.. not su
or sub
.