Jonathan Mui
Jonathan Mui

Reputation: 2511

Namespace module class methods undefined

I'm trying to use modules for namespacing reasons. I have this file located in my Rails app at /lib/reports/stripe.rb.

module Reports
  module Stripe
    def self.foo
      puts 'i am foo'
    end
  end
end

In my console, I'd expect to be able to call foo by Reports::Stripe.foo or Reports::Stripe::foo, but I get the error

NoMethodError: undefined method `foo' for Reports::Stripe:Module

What am I doing wrong? Also feel free to let me know if there's a better way to organize the location and namespacing.

Upvotes: 1

Views: 879

Answers (2)

Jonathan Mui
Jonathan Mui

Reputation: 2511

The file was actually located in /lib/reports/stripe/stripe.rb. It was a mistake I made much earlier, but forgot to fix. Moving the file to /lib/reports/stripe.rb resolved the issue.

Upvotes: 0

max
max

Reputation: 101811

All method calls in ruby use the . syntax. Even "module" methods.

> Reports::Stripe.foo
i am foo

You may be receiving the error NoMethodError: undefined method 'foo' for Reports::Stripe:Module if you have added the method after you have started the rails console. Try restarting the console or reloading the file with load 'reports/stripe'.

Upvotes: 2

Related Questions