Newbie question: Referencing a class inside another class

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

On 8/01/2013, at 8:08 AM, “brinda M.” [email protected] 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 B. baz”
end
end
end

Bar.new.baz
NameError: uninitialized constant Bar

Use the fully qualified name

Foo::Bar.new.baz
“Foo B. baz”
=> nil

Henry

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 M. wrote in post #1091358:

On 8/01/2013, at 8:08 AM, “brinda M.” [email protected] 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 B. baz”
end
end
end

Bar.new.baz
NameError: uninitialized constant Bar

Use the fully qualified name

Foo::Bar.new.baz
“Foo B. baz”
=> nil

Henry

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs