Ruby Alias and module_function
- by Jesse J
I'm trying to debug someone else's code and having trouble figuring out what's wrong. When I run rake, one of the errors I get is:
2) Error:
test_math(TestRubyUnits):
NoMethodError: undefined method `unit_sin' for CMath:Module
/home/user/ruby-units/lib/ruby_units/math.rb:21:in `sin'
This is the function that calls the method:
assert_equal Math.sin(pi), Math.sin("180 deg".unit)
And this is what the class looks like:
module Math
alias unit_sin sin
def sin(n)
Unit === n ? unit_sin(n.to('radian').scalar) : unit_sin(n)
end
alias unit_cos cos
def cos(n)
Unit === n ? unit_cos(n.to('radian').scalar) : unit_cos(n)
end
...
module_function :unit_sin
module_function :sin
module_function :unit_cos
module_function :cos
...
end
(The ellipsis means "more of the same"). As far as I can see, this is valid Ruby code. Is there something I'm missing here that's causing the error, or could the error be coming from something else?
Update: I'm wondering if the problem has to do with namespaces. This code is attempting to extend CMath, so perhaps the alias and/or module_function isn't actually getting into CMath, or something like that....