Class Names

Hi, I was wondering how I can reffer to a class outside of my class, but
it has the same name. Hopefully the following code will make it more
clear:

module Test
class File
def print_file
# I don’t want to call Test::File.open here:
File.open(‘test.rb’, “r”) { |f| puts f.read }
end
end
end

Test::File.new().print_file

On 6/22/06, Aaron P. [email protected] wrote:

end
end

Test::File.new().print_file

Prefix with ::, eg ::File.open.

Aaron P. wrote:

Hi, I was wondering how I can reffer to a class outside of my class, but
it has the same name. Hopefully the following code will make it more
clear:

module Test
class File
def print_file
# I don’t want to call Test::File.open here:
File.open(‘test.rb’, “r”) { |f| puts f.read }

    ::File.open('test.rb', "r") { |f| puts f.read }

The :: means look up the constant in the top level scope.

On Thu, Jun 22, 2006 at 08:01:48AM +0900, Phillip H. wrote:

Prefix with ::, eg ::File.open.

Perfect. Thank you!

–Aaron

Aaron P. wrote:

Hi, I was wondering how I can reffer to a class outside of my class, but
it has the same name. Hopefully the following code will make it more
clear:

module Test
class File
def print_file
# I don’t want to call Test::File.open here:
File.open(‘test.rb’, “r”) { |f| puts f.read }

::File.open(‘test.rb’, “r”) { |f| puts f.read }

end

end
end

Test::File.new().print_file

HTH,
Dave

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