Question

Suppose I have a module, mod.rb

# mod.rb

def foo
  puts 'it'
end

And in another ruby file, main.rb I require it and use the function foo.

# main.rb

require('mod')

foo

When requiring multiple modules, this can make code hard to read because I don't really know where 'foo' is coming from. Is there any idiomatic way to namespace these things in Ruby for the purpose of readability? I am new to the language, and contributing to a project. It took a lot of investigative work to find out which methods came from where. If I wanted to contribute more by improving the readability, what would be a good approach?

Was it helpful?

Solution

You will want to wrap your code in an actual module or class

something like

class Foo
  def self.bar
    "quxx"
  end
end

This way, you will be able to call Foo.bar to access your method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top