deprecated

Discussion in 'Ruby' started by Robert Klemme, Feb 25, 2008.

  1. 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 Klemme, Feb 25, 2008
    #1
    1. Advertising

  2. Robert Klemme wrote:
    >
    > 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


    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

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Feb 25, 2008
    #2
    1. Advertising

  3. On 25.02.2008 23:02, Joel VanderWerf wrote:
    > Robert Klemme wrote:
    >> 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

    >
    > 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


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

    Kind regards

    robert
     
    Robert Klemme, Feb 26, 2008
    #3
  4. Thanks for this code :d
     
    Stéphane Wirtel, Feb 26, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Barney Barumba
    Replies:
    0
    Views:
    564
    Barney Barumba
    Jul 23, 2003
  2. david
    Replies:
    4
    Views:
    6,343
    david
    Oct 9, 2003
  3. David McDivitt

    deprecated getResources method

    David McDivitt, May 18, 2004, in forum: Java
    Replies:
    2
    Views:
    1,878
    David McDivitt
    May 19, 2004
  4. Bernd Oninger
    Replies:
    1
    Views:
    413
    GIMME
    Jun 10, 2004
  5. kaeli
    Replies:
    3
    Views:
    4,911
    Eric Sosman
    Aug 24, 2004
Loading...

Share This Page