Pradhan
Pradhan

Reputation: 421

Ruby: Multiple module declaration

I am trying to understand a piece of code with multiple modules

module a; module b; module c;

def foo
    #something 
end

end
end
end

So what exactly does the above code mean? Is it like all 3 modules have foo and I can access a.foo or b.foo etc..?

Upvotes: 2

Views: 1942

Answers (1)

d11wtq
d11wtq

Reputation: 35318

I can see how that would be confusing. It is very badly laid out. Split the module declarations onto separate lines and it makes more sense. Ruby allows some things to be separated by semicolons if written on one line... that is being abused here and just leads to confusion.

The code expands to this, when correctly laid out:

module a
  module b
    module c
      def foo
        #something 
      end
    end
  end
end

Other examples of using a semicolon to write a definition on a single line include:

class Foo < Bar; def zip; if @terrifying; puts "Yes"; else; puts "No"; end; end; end

You can probably see why I say this isn't well written. Also, the module names should begin with an Uppercase letter.

Upvotes: 3

Related Questions