Deprecated

This just occurred to me (for Java fans):

class Module
def deprecated *meths
meths.each do |me|
aliased = “deprecate#{me}”
alias_method aliased, me
class_eval “def #{me}(*a,&b) $stderr.puts ‘WARNING: deprecated
method called: #{me}’; #{aliased}(*a,&b) end”
end
end
end

irb(main):010:0> class String
irb(main):011:1> deprecated :to_i
irb(main):012:1> end
=> [:to_i]
irb(main):013:0> “123”.to_i
WARNING: deprecated method called: to_i
=> 123
irb(main):014:0>

Kind regards

robert

Robert K. wrote:

end

end
end

Nice (and I’m not a Java fan). I’d make the output indirect, though, in
case of GUI etc. Also, you could use this to only warn on the first call
of each method for a given class.

class Module
def deprecated *meths
meths.each do |me|
aliased = “deprecate#{me}”
alias_method aliased, me
class_eval %{
def #{me}(*a,&b)
warn_deprecated(’#{me}’)
#{aliased}(*a,&b)
end
}
end
end
end

class Object
def warn_deprecated me
warn “WARNING: deprecated method called: #{me}”
end
end

On 25.02.2008 23:02, Joel VanderWerf wrote:

end
   aliased = "_deprecate_#{me}"

class Object
def warn_deprecated me
warn “WARNING: deprecated method called: #{me}”
end
end

Good idea! This could even be combined with standard logging libs.

Kind regards

robert