Thought I'd share my alias

class Module

As with alias_method, but alias both reader and writer.

attr_accessor :x

self.x = 1

alias_accessor :y, :x

y #=> 1

self.y = 2

x #=> 2

def alias_accessor(*args)
orig = args.last
args = args - [orig]
args.each do |name|
alias_method(name, orig)
alias_method("#{name}=", “#{orig}=”)
end
end

def alias_reader(*args)
orig = args.last
args = args - [orig]
args.each do |name|
alias_method(name, orig)
end
end

def alias_writer(*args)
orig = args.last
args = args - [orig]
args.each do |name|
alias_method("#{name}=", “#{orig}=”)
end
end

end

On Nov 10, 6:28 am, Trans [email protected] wrote:

orig = args.last
  alias_method("#{name}=", "#{orig}=")
end

end

end

Okay. I’d say shorten

orig = args.last
args = args - [orig]

to ‘orig = args.pop’.

Actually, if I were using this myself I would write

def alias_accessor(orig, first_alias, *aliases)
aliases.unshift(first_alias)
aliases.each …

which is more idiomatic, I think, for variadic methods. I don’t think
imitation of alias_method()'s parameter order is worth the decrease in
elegance.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs