Forum: Ruby Newbie question: Referencing a class inside another class

Posted by brinda M. (bindu_m)
on 2013-01-07 20:08
I have an Error class defined inside another class in the lib directory
(i.e: Class SampleError is defined inside class
lib/third_party_api/Sample class). How do I reference SampleError from
outside
the lib directory (app/models/)? I need to specify the errors to retry
to retry as follows:  @retry_exceptions = [SampleError].
Posted by Henry Maddocks (Guest)
on 2013-01-07 21:30
(Received via mailing list)
On 8/01/2013, at 8:08 AM, "brinda M." <lists@ruby-forum.com> wrote:

> I have an Error class defined inside another class in the lib directory
> (i.e: Class SampleError is defined inside class
> lib/third_party_api/Sample class). How do I reference SampleError from
> outside

 class Foo
   class Bar
     def baz
       p "Foo Bar baz"
     end
   end
 end


Bar.new.baz
NameError: uninitialized constant Bar

Use the fully qualified name

Foo::Bar.new.baz
"Foo Bar baz"
=> nil

Henry
Posted by brinda M. (bindu_m)
on 2013-01-07 23:04
Thanks for the reply.
However I should have been clear about what I meant. Sorry! The class is 
defined as follows:

class SampleError
end

class Sample
end

They are defined in file lib/pkg/sample.rb.

How do I reference SampleError from app/models/caller.rb since 
SampleError is defined inside sample.rb?
Thanks!

Henry Maddocks wrote in post #1091358:
> On 8/01/2013, at 8:08 AM, "brinda M." <lists@ruby-forum.com> wrote:
>
>> I have an Error class defined inside another class in the lib directory
>> (i.e: Class SampleError is defined inside class
>> lib/third_party_api/Sample class). How do I reference SampleError from
>> outside
>
>  class Foo
>    class Bar
>      def baz
>        p "Foo Bar baz"
>      end
>    end
>  end
>
>
> Bar.new.baz
> NameError: uninitialized constant Bar
>
> Use the fully qualified name
>
> Foo::Bar.new.baz
> "Foo Bar baz"
> => nil
>
> Henry
Posted by Calvin B. (calvin_b)
on 2013-01-07 23:37
(Received via mailing list)
Hello,

if you have required the content of lib/pkg/sample.rb into
app/models/caller.rb, you can simply reference the class SampleError by
its name. It is defined in the global namespace, unless in sample.rb the
classes are in another module (in that case you have to proceed as Henry
told you). To require the content, just use one of the functions in the
require family in the top of your file caller.rb. This would look
something like the following:

   require_relative '../../lib/pkg/sample.rb'

Note: require_relative requires the file relative from the file it's
written in, unlike require, which looks through a specified set of
directories ($LOAD_PATH) and takes the file from there.

regards,

Calvin
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.