Loading ...
Sorry, an error occurred while loading the content.

Re: [id-ruby] cara meng-include class method di module ?

Expand Messages
  • rahmat budiharso
    Kayaknya begini bisa deh :D module Penting def self.included(base) base.extend ClassMethods base.class_eval do puts init lho end base.send :include,
    Message 1 of 2 , Feb 10, 2011
    • 0 Attachment
      Kayaknya begini bisa deh :D

      module Penting

      def self.included(base)
      base.extend ClassMethods
      base.class_eval do
      puts "init lho"
      end
      base.send :include, InstanceMethods
      end # self.included

      module ClassMethods
      def satu
      puts 'satu'
      end

      def dua
      puts 'dua'
      end
      end # ClassMethods

      module InstanceMethods
      def tiga
      puts 'tiga, ini instance method lho'
      end
      end # InstanceMethods

      end

      class CobaCoba
      include Penting
      end

      CobaCoba.satu
      CobaCoba.dua
      c = CobaCoba.new
      c.tiga


      (\ /)
      (O.o)
      (> <)

      This is Bunny. Copy Bunny into your signature to help him on his way to
      world domination.




      ________________________________
      From: Aditya Agustyana <aditya.jamop@...>
      To: id-ruby <id-ruby@yahoogroups.com>
      Sent: Fri, February 11, 2011 11:13:15 AM
      Subject: [id-ruby] cara meng-include class method di module ?


      dear rubyist

      saya punya class seperti ini

      class Computer
      self.satu
      "ini satu"
      end

      self.dua
      "ini dua"
      end

      def nyetrum
      "mati"
      end
      end

      kalau diexecute Computer.satu tentulah akan menghasilkan "ini satu" dan kalo
      execute Computer.new.nyetrum tentu akan menghasilkan "mati"

      nah katakanlah setelah develop bbrp hari class methods & instance methods di
      class Computer bertambah banyak, sehingga untuk mempermudah mengelompokan
      saya ingin memindahkan bbr class/instance methods tsb ke sebuah module,
      misalnya general.rb

      module General
      self.satu
      "ini satu"
      end

      self.dua
      "ini dua"
      end

      def nyetrum
      "mati"
      end

      end

      tentu saja di computer.rb kodenya berubah seperti ini

      require "general.rb"

      class Computer
      include General

      ## di bawah ini kumpulan class/instance methods lain
      end

      nah jika saya mengexecute Computer.new.nyetrum tidak terjadi error, tetapi
      kalau saya execute Computer.satu ini menyebabkan error

      dgn msg sbg berikut
      undefined method `satu' for Computer:Class

      nah pertanyaanya gmn yah caranya supaya kalo sy execute Computer.satu tidak
      muncul error tanpa sy harus memindahkanan kembali self.satu dari general.rb
      ke computer.rb

      mohon pencerahannya

      --
      profile : http://about.me/aditya.agustyana
      ym / twitter : kirconboy
      skype : adit_skype

      Be Nice. Treat others with the same respect you'd want them to treat you.
      We're all here to learn together. Be tolerant of others who may not know
      everything you know. BRING YOUR SENSE OF HUMOR (stackoverflow.com)

      [Non-text portions of this message have been removed]







      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.