Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Different ways to invoke method in ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 280
    Comment on it

    Ruby provide with the following ways by which you can invoke the method based on your requirement :

    s= "ruby method"
    

    1) Call a public instance method directly

    p s.length #=> 11
    

    2) Other way is to invoke a method dynamically in ruby is to send a message to the object :

    p s.send(:length) #=> 11
    

    3) Other way is to instantiate a method object and then call it:

    object = s.method(:length) 
       p object.call #=> 11
    

    4) And the last way is to use the eval method:

    eval "s.length" #=> 11
    

    Well instantiating a method object is the fastest dynamic way of calling a method and eval is the slowest.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: