Difference between length and length()

Hi,

Is there any difference between length and length().Pls explain

Nike M. wrote:

Is there any difference between length and length().Pls explain

In general,

foo.length
and
foo.length()

are the same thing. They are calling a method ‘length’ on object ‘foo’
with no arguments.

However, a bareword ‘length’ with no receiver could be a local variable.
Try the following in irb:

def length
return 123
end

length = 456
length # this is 456, not a call to method ‘length’
length() # this calls the method ‘length’
self.length # so does this

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs