Let's say we have a bunch of methods with typical prefixes.
def pref_foo
# code
end
def pref_bar
# code
end
I want to learn how to automatically prepend those prefixes to my method names (like how it's done in Rails: Model.find_by_smth
).
In other words I want to create some scope pref_
, that takes methods and prepends pref_
to their names, so my method foo
is becoming available as pref_foo
.
module Bar
# definition of some wrapper `pref_`
end
class Foo
include Bar
<some wrapper from Bar> do
def foo
puts 'What does a computer scientist wear on Halloween?'
end
def bar
puts 'A bit-mask.'
end
end
end
foo = Foo.new
foo.pref_foo # => "What does a computer scientist wear on Halloween?"
foo.pref_bar # => "A bit-mask."
Try this,
How does this work?
with_prefix
on the superclass of all classesdef
statements on the anonymous module rather than the class