IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Coming at it from the other way
In Python, one can add functions to a class on the fly:

\n>>> class MyClass:\n... \tpass\n... \n>>> a = MyClass()\n>>> a.test()\nTraceback (most recent call last):\n  File "<interactive input>", line 1, in ?\nAttributeError: MyClass instance has no attribute 'test'\n>>> def myMethod(self, name):\n... \tprint "Hi from " + name\n... \t\n>>> myMethod(a, "stand alone")\nHi from stand alone\n>>> MyClass.test = myMethod \n>>> a.test("a class")\nHi from a class\n


Can this be done in a method oriented language like Ruby?

Regards,
John
New Adding new methods in Ruby
Classes are always open in Ruby. New methods can be added at any time.

    # Create a new class\n    class MyClass\n    end\n\n    # Create an instance\n    a = MyClass.new\n\n    # Send a message for a non-existent method\n    a.f             # => NoMethodError: undefined method `f' for #<MyClass:0x1a1810>\n\n    # Open the class and add a new method\n    class MyClass\n      def f(name)\n        puts "Hi from #{name}"\n      end\n    end\n\n    # Now the instance will recognize the message\n    a.f("a class")  # => "hi from a class"
--
-- Jim Weirich jweirich@one.net [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
     MOO/FOO and Python - (JimWeirich) - (11)
         Having done this before... - (admin) - (10)
             I'm not sure that's what I mean - (JimWeirich) - (9)
                 Like this: - (admin) - (4)
                     :) I haven't "gone meta" enough yet, I see... -NT - (FuManChu)
                     I came up with this ... - (JimWeirich) - (2)
                         I ran mine in 2.2.3, FYI - (admin) - (1)
                             My 2.2 is evidently broken - (JimWeirich)
                 AFAICT that hinges on method_missing... - (FuManChu)
                 You could also do this: - (admin)
                 Coming at it from the other way - (johnu) - (1)
                     Adding new methods in Ruby - (JimWeirich)

At the end of the day, it's still North Dakota.
40 ms