Object.__send__

Posted: April 5, 2007 in RUBY

metode send maupun __send__ dari Object adalah cara eksplisit untuk mengirim objek ke Kelas, atau dengan kata lain memanggil metode untuk objek.

sayang:~ arie$ irb
Arigatou jyanai! Pizza wa (2) ni-mai, OK!
irb(main):001:0> system 'Object.__send__'
=> false
irb(main):002:0> system 'fri Object.__send__'
-------------------------------------------------------- Object#__send__
     obj.send(symbol [, args...])        => obj
     obj.__send__(symbol [, args...])    => obj
------------------------------------------------------------------------
     Invokes the method identified by symbol, passing it any arguments 
     specified. You can use __send__ if the name send clashes with an 
     existing method in obj.

        class Klass
          def hello(*args)
            "Hello " + args.join(' ')
          end
        end
        k = Klass.new
        k.send :hello, "gentle", "readers"   #=> "Hello gentle readers"

=> true
irb(main):003:0> class A
irb(main):004:1>   def yo(*cih)
irb(main):005:2>     cih.join ' '
irb(main):006:2>   end
irb(main):007:1> end
=> nil
irb(main):008:0> a = A.new
=> #<A>
irb(main):009:0> a.object_id
=> 131890
irb(main):010:0> a.send :yo, 1, 2, 3
=> "1 2 3"
irb(main):011:0> a.yo(1,2,3)
=> "1 2 3"

tinggalkan feedback konstruktif atau lebih baik diam

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s