Alexandre
Alexandre

Reputation: 13308

Operator & in ruby

There is a code in ruby

Dir.glob("my_folder/*.rb").each { |r| require_relative r}

I almost understand but I want to be sure why is the code below not working

Dir.glob("my_folder/*.rb").each(&:require_relative)

due to error of NoMethodError: private method require_relative' called for "my_folder/one.rb":String

Is this because

Dir.glob("controllers/*.rb").each(&:require_relative)

is equal to

Dir.glob("controllers/*.rb").each{ |r| r.require_relative }

?

Upvotes: 1

Views: 76

Answers (1)

manojlds
manojlds

Reputation: 301147

You are right, it is equivalent to

 .each{ |r| r.require_relative}

& calls to_proc on an object, in this case, a symbol, and Symbol implements it and creates a new Proc, which does a call on the object.

Upvotes: 2

Related Questions