Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you need parenthesis around a Ruby require

Tags:

ruby

In Ruby, is require('my_thing') equivalent to require 'my_thing'? It's just the ruby convention that you don't need to wrap a function's arguments in paranetheses right?

like image 386
Don P Avatar asked Jan 29 '26 16:01

Don P


2 Answers

You are right, you don't need them, however, I think you need to know one important thing before deciding to ditch them all-together in your method calls (regarding your question on conventions):

Follow Best Practices

According to the Ruby Style Guide:

Omit parentheses around parameters for methods that are part of an internal DSL (e.g. Rake, Rails, RSpec), methods that have "keyword" status in Ruby (e.g. attr_reader, puts) and attribute access methods. Use parentheses around the arguments of all other method invocations.

Since require has a "keyword" status in Ruby, it's okay to not use parentheses. If, however, you made your own my_require method, then it would be a better idea to use them.

like image 72
daremkd Avatar answered Jan 31 '26 20:01

daremkd


Allowing omission of parentheses around the argument is a language feature, not a convention.

It is a convention to omit parentheses for methods defined on the Kernel module that start with lower case. require is one of them.

like image 32
sawa Avatar answered Jan 31 '26 20:01

sawa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!