Module issue

J

Jason Vogel

Disclaimer: Ruby Nuby

Source

module Test # (filename my_test.rb)

def test
puts "\n\ntest fired\n\n"
end
end

class OfferRenewalController < ApplicationController

def payment_made

require 'my_test'

Test.test

end

Error
NoMethodError in Offer renewalController#payment_made

private method `test' called for Test:Module

So what am I doing wrong?

Thanks,
Jason
 
V

Vincent Fourmond

Jason said:
Disclaimer: Ruby Nuby

Source

module Test # (filename my_test.rb)

def test
puts "\n\ntest fired\n\n"
end
end

class OfferRenewalController < ApplicationController

def payment_made

require 'my_test'

Test.test

end

Error
NoMethodError in Offer renewalController#payment_made

private method `test' called for Test:Module

So what am I doing wrong?

In the module, you did define test as an instance method. So if you
want to use it, you should

include Test

If you want to use as Test.test, define it as

module Test
def Test.test
[...]

or

module Test
def self.test
[...]

The latter is better as if you decide to change the name of your module,
you won't have to change the names of methods.

By the way, I find it funny that you require 'my_test' from within a
method. I believe it would be better to write

require 'my_test'

class OfferRenewalController < ApplicationController
def payment_made
Test.test
end
end

Cheers,

Vince
 
J

Jason Vogel

I plan move the "require" up top, once I've got my stuff straight.

Okay, I needed the "self." in the Module def.

< in ahs.rb >
module Ahs
class CreditCard
def self.test2
puts "\n\nself.CreditCard.test2 fired\n\n"
end
end
def self.test
puts "\n\nself.test fired\n\n"
end
end

< in offer_renewal_controller.rb >
Ahs.test
Ahs.CreditCard.test2

Result:
self.test fired
undefined method `CreditCard' for Ahs:Module [exception thrown]

What's wrong with the second statement?

Thanks helping,
Jason

Jason said:
Disclaimer: Ruby Nuby

module Test # (filename my_test.rb)
def test
puts "\n\ntest fired\n\n"
end
end
class OfferRenewalController < ApplicationController
def payment_made
require 'my_test'


Error
NoMethodError in Offer renewalController#payment_made
private method `test' called for Test:Module
So what am I doing wrong? In the module, you did define test as an instance method. So if you
want to use it, you should

include Test

If you want to use as Test.test, define it as

module Test
def Test.test
[...]

or

module Test
def self.test
[...]

The latter is better as if you decide to change the name of your module,
you won't have to change the names of methods.

By the way, I find it funny that you require 'my_test' from within a
method. I believe it would be better to write

require 'my_test'

class OfferRenewalController < ApplicationController
def payment_made
Test.test
end
end

Cheers,

Vince
 
S

Stefano Crocco

Alle 18:55, venerd=EC 8 dicembre 2006, Jason Vogel ha scritto:
I plan move the "require" up top, once I've got my stuff straight.

Okay, I needed the "self." in the Module def.

< in ahs.rb >
module Ahs
class CreditCard
def self.test2
puts "\n\nself.CreditCard.test2 fired\n\n"
end
end
def self.test
puts "\n\nself.test fired\n\n"
end
end

< in offer_renewal_controller.rb >
Ahs.test
Ahs.CreditCard.test2

Result:
self.test fired
undefined method `CreditCard' for Ahs:Module [exception thrown]

What's wrong with the second statement?

You should use Ahs::CreditCard, not Ahs.CreditCard. The . syntax is used to=
=20
call module methods; the :: is used to access constants defined in a module=
=20
(including classes).
 
J

Jason Vogel

Sweet, that works.

Thanks Vincent and Stefano,
Jason

Alle 18:55, venerdì 8 dicembre 2006, Jason Vogel ha scritto:


I plan move the "require" up top, once I've got my stuff straight.
Okay, I needed the "self." in the Module def.
< in ahs.rb >
module Ahs
class CreditCard
def self.test2
puts "\n\nself.CreditCard.test2 fired\n\n"
end
end
def self.test
puts "\n\nself.test fired\n\n"
end
end
< in offer_renewal_controller.rb >
Ahs.test
Ahs.CreditCard.test2
Result:
self.test fired
undefined method `CreditCard' for Ahs:Module [exception thrown]
What's wrong with the second statement?You should use Ahs::CreditCard, not Ahs.CreditCard. The . syntax is used to
call module methods; the :: is used to access constants defined in a module
(including classes).
 
A

Andrew Libby

Hi Jason,

Try

Class OfferRenewallController < ApplicationController
include Test

def payment_made
test
end
end

I _think_ this is what you're looking for.

Good luck.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,122
Members
47,743
Latest member
henrywalker

Latest Threads

Top