Skip to main content
Post Reopened by Simon Forsberg, Marc-Andre, syb0rg, rolfl, Uri Agassi
added 462 characters in body; edited title
Source Link

Unable to Call Calling a Class Method on a Module

I have a module (in file dialect.rb) defined as such:

require 'dialect/generators/elements'

module Dialect
  def self.included(caller)
    caller.extend Dialect::Generator::Element
  end

  def self.version
    "Dialect v#{Dialect::VERSION}"
  end
end

Then I have the file dialect/generators/elements.rb, which looks like this:

module Dialect
  module Generator
    module Element

      puts Dialect.version

    end
  end
end

If I run my app, I get:

/lib/dialect/generators/elements.rb:7:in `<module:Element>':
undefined method `version' for Dialect:Module (NoMethodError)

My question/problem is: I don'tdidn't understand why the Element module can'tcould not find the version method here.

Incidentally, howHow I call Dialect is like this:

require 'dialect'

class PageTest
  include Dialect
end

So you can see Dialect is mixed-in to an existing class. It's when this class is instantiated that I get the error above.

When I try a simple IRB session doing what appears to be this same logic, this all seems to work:

irb(main):001:0> module Dialect
irb(main):002:1>   def self.version
irb(main):003:2>     puts "Version number"
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> module Dialect
irb(main):007:1>   module Generator
irb(main):008:2>     module Element
irb(main):009:3>       puts Dialect.version
irb(main):010:3>     end
irb(main):011:2>   end
irb(main):012:1> end
Version number

Here I get the "Version number" text back, which tells me (I think?) that Dialect::Generator::Element can call the method version on Dialect.

So I haveThe issue ended up being corrected by simply moving my require statement to believethe end, like this:

module Dialect
  def self.included(caller)
    caller.extend Dialect::Generator::Element
  end

  def self.version
    "Dialect v#{Dialect::VERSION}"
  end
end

require 'dialect/generators/elements'

Having the require statement at the end solves the problem I was having.

The question then becomes: is somethingthis a good way to do with how I'm including Dialect, butthis? I don't seefeel like making my logic depend on where the issuerequire statement goes seems like a bad idea.

Unable to Call Class Method on Module

I have a module (in file dialect.rb) defined as such:

require 'dialect/generators/elements'

module Dialect
  def self.included(caller)
    caller.extend Dialect::Generator::Element
  end

  def self.version
    "Dialect v#{Dialect::VERSION}"
  end
end

Then I have the file dialect/generators/elements.rb, which looks like this:

module Dialect
  module Generator
    module Element

      puts Dialect.version

    end
  end
end

If I run my app, I get:

/lib/dialect/generators/elements.rb:7:in `<module:Element>':
undefined method `version' for Dialect:Module (NoMethodError)

My question/problem is: I don't understand why the Element module can't find the version method here.

Incidentally, how I call Dialect is like this:

require 'dialect'

class PageTest
  include Dialect
end

So you can see Dialect is mixed-in to an existing class. It's when this class is instantiated that I get the error above.

When I try a simple IRB session doing what appears to be this same logic, this all seems to work:

irb(main):001:0> module Dialect
irb(main):002:1>   def self.version
irb(main):003:2>     puts "Version number"
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> module Dialect
irb(main):007:1>   module Generator
irb(main):008:2>     module Element
irb(main):009:3>       puts Dialect.version
irb(main):010:3>     end
irb(main):011:2>   end
irb(main):012:1> end
Version number

Here I get the "Version number" text back, which tells me (I think?) that Dialect::Generator::Element can call the method version on Dialect.

So I have to believe this is something to do with how I'm including Dialect, but I don't see the issue.

Calling a Class Method on a Module

I have a module (in file dialect.rb) defined as such:

require 'dialect/generators/elements'

module Dialect
  def self.included(caller)
    caller.extend Dialect::Generator::Element
  end

  def self.version
    "Dialect v#{Dialect::VERSION}"
  end
end

Then I have the file dialect/generators/elements.rb, which looks like this:

module Dialect
  module Generator
    module Element

      puts Dialect.version

    end
  end
end

If I run my app, I get:

/lib/dialect/generators/elements.rb:7:in `<module:Element>':
undefined method `version' for Dialect:Module (NoMethodError)

My question/problem is: I didn't understand why the Element module could not find the version method here.

How I call Dialect is like this:

require 'dialect'

class PageTest
  include Dialect
end

So you can see Dialect is mixed-in to an existing class. It's when this class is instantiated that I get the error above.

When I try a simple IRB session doing what appears to be this same logic, this all seems to work:

irb(main):001:0> module Dialect
irb(main):002:1>   def self.version
irb(main):003:2>     puts "Version number"
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> module Dialect
irb(main):007:1>   module Generator
irb(main):008:2>     module Element
irb(main):009:3>       puts Dialect.version
irb(main):010:3>     end
irb(main):011:2>   end
irb(main):012:1> end
Version number

Here I get the "Version number" text back, which tells me (I think?) that Dialect::Generator::Element can call the method version on Dialect.

The issue ended up being corrected by simply moving my require statement to the end, like this:

module Dialect
  def self.included(caller)
    caller.extend Dialect::Generator::Element
  end

  def self.version
    "Dialect v#{Dialect::VERSION}"
  end
end

require 'dialect/generators/elements'

Having the require statement at the end solves the problem I was having.

The question then becomes: is this a good way to do this? I feel like making my logic depend on where the require statement goes seems like a bad idea.

Post Closed as "Not suitable for this site" by Yuushi, amon, Uri Agassi, syb0rg, Fge
Source Link

Unable to Call Class Method on Module

I have a module (in file dialect.rb) defined as such:

require 'dialect/generators/elements'

module Dialect
  def self.included(caller)
    caller.extend Dialect::Generator::Element
  end

  def self.version
    "Dialect v#{Dialect::VERSION}"
  end
end

Then I have the file dialect/generators/elements.rb, which looks like this:

module Dialect
  module Generator
    module Element

      puts Dialect.version

    end
  end
end

If I run my app, I get:

/lib/dialect/generators/elements.rb:7:in `<module:Element>':
undefined method `version' for Dialect:Module (NoMethodError)

My question/problem is: I don't understand why the Element module can't find the version method here.

Incidentally, how I call Dialect is like this:

require 'dialect'

class PageTest
  include Dialect
end

So you can see Dialect is mixed-in to an existing class. It's when this class is instantiated that I get the error above.

When I try a simple IRB session doing what appears to be this same logic, this all seems to work:

irb(main):001:0> module Dialect
irb(main):002:1>   def self.version
irb(main):003:2>     puts "Version number"
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> module Dialect
irb(main):007:1>   module Generator
irb(main):008:2>     module Element
irb(main):009:3>       puts Dialect.version
irb(main):010:3>     end
irb(main):011:2>   end
irb(main):012:1> end
Version number

Here I get the "Version number" text back, which tells me (I think?) that Dialect::Generator::Element can call the method version on Dialect.

So I have to believe this is something to do with how I'm including Dialect, but I don't see the issue.