In ruby, you can do this:
class Thing
public
def f1
puts "f1"
end
private
def f2
puts "f2"
end
public
def f3
puts "f3"
end
private
def f4
puts "f4"
end
end
where now f1 and f3 and public, f2 and f4 is private. What is going on internally that allows you to invoke a class method that then changes the method definition? How can I implement the same functionality (ostensibly to create my own java like annotations)
for example…
class Thing
fun
def f1
puts "hey"
end
notfun
def f2
puts "hey"
end
end
and fun and notfun would change the following function definitions.
Thanks
You can sometimes shove Ruby into an espressso cup. Let’s see how.
Here’s a module FunNotFun…
… that you can use to extend a class …
… with this result:
But see below the line for a better way.
Annotations (see normalocity’s answer) are less trouble and, being a common Ruby idiom, will more easily communicate your code’s intent. Here’s how to do it with annotations:
In use, the annotation comes after the method is defined, rather than before:
The result is the same: