Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby's counterpart to python 's __init__

Tags:

ruby

Python has modules and packages, and packages have __init__.py file, so if I import a package called foo, the interpreter will look for foo/__init__.py.

I came across some ruby code that will include not a module but a directory: require 'core/utilts'. The core and utilts are directories, not single module files. What is the equivalent in ruby to the file __init__.py, or what are the files that are executed when I require a directory like this utilts dir? There are a lot of ruby files under it.

like image 321
user3718463 Avatar asked Dec 20 '22 10:12

user3718463


1 Answers

You can't import directories in Ruby. If you're running require 'core/utils', then there is a file called core/utils.rb. There might also be a directory called core/utils/ but it's utils.rb that is being included via require, and it will in turn likely includes files out of core/utils/.

So, the answer to your question is: No, there is no equivalent to foo/__init__.py, but the same thing can typically be accomplished with foo.rb which includes additional files in foo/.

like image 181
meagar Avatar answered Jan 07 '23 18:01

meagar