Calling instance method when module included

Discussion in 'Ruby' started by Leon Bogaert, May 28, 2008.

  1. Leon Bogaert

    Leon Bogaert Guest

    Hi all,

    Maybe a silly question, but is it possible to call instance methods on
    an object when you've included a module? Or maybe I'm having the wrong
    approach and somebody can point me in the right direction?

    I'm now doing something like:

    a = ['one', 'two', 'three']
    a.send:)include, InstanceMethods)

    And the InstanceMethods module looks like:

    module InstanceMethods

    def self.included(base)
    base.instance_eval do
    self.each do |item|
    p item
    end
    end
    end
    end

    Now I'm getting everytime: NoMethodError: undefined method `each' for
    Array:Class

    So my question: is a construction like this possible? If yes, how then?
    If no: is there a better way to achieve this then? Or should I drop the
    idea and first include the module and THEN create the object?

    In this context it's maybe a bit silly, but I didn't want to post the
    code in which I'm trying to use this construct because then it maybe
    distracts from the idea I'm trying to achieve.

    Thanks in advance all!
    --
    Posted via http://www.ruby-forum.com/.
    Leon Bogaert, May 28, 2008
    #1
    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. Galevsky Gal

    Class instance into included module

    Galevsky Gal, Jun 26, 2007, in forum: Ruby
    Replies:
    8
    Views:
    119
    Galevsky gal
    Jun 26, 2007
  2. Yu-shan Fung
    Replies:
    9
    Views:
    136
    marco antonio f.
    Mar 6, 2011
  3. Raj Singh
    Replies:
    2
    Views:
    187
    Rick DeNatale
    May 29, 2008
  4. Greg Hauptmann
    Replies:
    9
    Views:
    233
    Loren Segal
    Jun 16, 2008
  5. Prince Nez
    Replies:
    3
    Views:
    241
    Robert Klemme
    Aug 18, 2009
Loading...

Share This Page