Disclaimer: Ruby N.
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
Jason V. wrote:
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
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
On Dec 8, 11:12 am, Vincent F. [email protected]
Sweet, that works.
Thanks Vincent and Stefano,
Jason
Alle 18:55, venerdì 8 dicembre 2006, Jason V. ha scritto:
end
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).
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