Is there a way to include a module to a class such that the module's methods override the methods of the class? For example:
module UpcasedName
def name
@name.upcase
end
end
class User
attr_accessor :name
include UpcasedName
end
u = User.new
u.name = 'john'
puts u.name # outputs 'john', not 'JOHN'
In the example above, u.name is 'john', not 'JOHN'. I know that if I extend the user object instead of including the module to the class, this will work
module UpcasedName
def name
@name.upcase
end
end
class User
attr_accessor :name
end
u = User.new
u.name = 'john'
u.extend UpcasedName
puts u.name # outputs 'JOHN'
However, I want to include the module at the class level, not object level.