[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Module issue

Jason Vogel

12/8/2006 4:52:00 PM

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

5 Answers

Vincent Fourmond

12/8/2006 5:12:00 PM

0

Jason Vogel wrote:
> 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

--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Jason Vogel

12/8/2006 5:53:00 PM

0

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 Fourmond <vincent.fourm...@9online.fr>
wrote:
> Jason Vogel wrote:
> > 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
>
> --
> Vincent Fourmond, PhD studenthttp://vincent.fourmon...

Stefano Crocco

12/8/2006 6:02:00 PM

0

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).

Jason Vogel

12/8/2006 6:13:00 PM

0

Sweet, that works.

Thanks Vincent and Stefano,
Jason

On Dec 8, 12:02 pm, Stefano Crocco <stefano.cro...@alice.it> wrote:
> 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).

Andrew Libby

12/9/2006 7:47:00 PM

0

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


Jason Vogel wrote:
> 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
>
>

--
Andrew Libby
Tangeis, LLC
Innovative IT Management Solutions
alibby@tangeis.com