A few months back, while porting some code from php to ruby, being a newbie that I am, I came across an issue.
The php code had a function as such:
public string functionName($var1, &$var2, $var3){}
Here the caller of the function gets to decide the name of the variable ($var2).
So, the caller can access the result from a variable name of the callers choice.
functionName("variablevalue", "variableName", "variableValue");
echo $variableName;
The code was procedural and had nothing to do with classes, methods or objects.
In ruby I had to provide a similar option where the caller can name the variable.
Here is how I had implemented this.
attr_accessor :userVar, :varName
Adding a couple of properties to the class. attr_accessor defines the get set methods.
def method_missing(meth, *x)
if "#{meth}" == self.varName
class << self
self
end.send :define_method, meth do |args|
self.userVar
end
self.send "#{meth}", x
else
puts "#{meth} : Such a method does not exist!"
end
end
This checks to see if the called method exists. Uses define_method .
def methodName(var1, userVar="", var3)
// function
self.userVar = userVar
self.varName = "#{self.userVar}"
self.userVar = self.result
return self.return
end
objectName.methodName(var1, "myvariable", var3) puts objectName.myVariable
The user input variable name is a new method/property which contains the result.





