Reputation: 131
I'm going through Why's Poignant Guide. I don't understand why 'require' in the first line is a method and not a variable:
require 'net/http'
Net::HTTP.start( 'www.ruby-lang.org', 80 ) do |http|
print( http.get( '/en/about/license.txt' ).body )
end
Upvotes: 0
Views: 133
Reputation: 1194
Because it invokes a Ruby message send, what is more commonly called a function in other languages. This particular 'function' loads an external piece of code referenced by the name 'net/http'. Sometimes syntactical elements such as parenthesies are omitted as a syntactical convenience, so it may not look exactly like other functions or Ruby message sends.
This description will not help you understand why this is the practice in Ruby because you need to learn a significant number of the syntactical patterns before realizing the intent of their design. Once you realize this design intent you have the answer to 'why' this syntax was chosen. Alternatively you could research the Ruby design discussions.
Upvotes: 1
Reputation: 230336
It can't be a variable, because there no such piece of language syntax that consists of a variable and a string literal separated by a space.
Ergo, this must be a method call (message send).
Upvotes: 1
Reputation: 369458
Because it takes an argument. In Ruby, only message sends have arguments.
Upvotes: 0
Reputation: 168101
The scope for a local variable is pretty much narrow. In case an expression is ambiguous between a local variable and a method, if you look within the current method body, class body, block, etc., and cannot find the local variable assignment, then it would be interpreted as a method.
Upvotes: 0