I'm reading through the Ruby documentation and books. I'm a bit confused
with two symbols (or operators?): double colon (: and pound sign (#).
I thought double colon '::' was a reference to a constant as in:
Math::PI
But I saw in some doc the following:
"You can create procs in several ways: with Proc::new or by calling either
the Kernel#lambda or Kernel#proc methods."
"Kernel#lambda" and "Kernel#proc" are quite understandable. They are
methods of the Kernel object.
"Proc::new" on the other hand, doesn't seem to point to a constant unless
methods can be constants as well (can they?).
They're used in documentation and discussion, but not in code.
* `Foo#bar` means the instance method 'bar' defined in class/module 'Foo',
invoked on its instances.
* `Baz::qux` means a method invoked on `Baz` itself.
e.g.:
···
On 14 April 2016 at 10:37, Marc Chanliau <marc.chanliau@gmail.com> wrote:
I'm reading through the Ruby documentation and books. I'm a bit confused
with two symbols (or operators?): double colon (: and pound sign (#).
I thought double colon '::' was a reference to a constant as in:
Math::PI
But I saw in some doc the following:
"You can create procs in several ways: with Proc::new or by calling either
the Kernel#lambda or Kernel#proc methods."
"Kernel#lambda" and "Kernel#proc" are quite understandable. They are
methods of the Kernel object.
"Proc::new" on the other hand, doesn't seem to point to a constant unless
methods can be constants as well (can they?).
Can somebody please explain?
~~~
foo = Foo.new
foo.bar # this is Foo#bar
Baz.qux # this is Baz::qux
~~~
Sometimes the double-colon is replaced with just a dot, since double-colon
does exist in syntax and means something else.
They're used in documentation and discussion, but not in code.
That's only true for the case of instance methods, see below.
* `Foo#bar` means the instance method 'bar' defined in class/module
'Foo', invoked on its instances.
* `Baz::qux` means a method invoked on `Baz` itself.
aka class method
Sometimes the double-colon is replaced with just a dot, since
double-colon does exist in syntax and means something else.
No. _Both_ double colon and dot can be used for class methods,
also in code:
But `::' is also used for name spacing, so that people often tend to
use a dot for invoking class methods (similar to the case of instance
methods), and not `::'; it's also shorter.
On the other hand - and here I mostly repeat what you already stated -,
`Foo#bar' is _only_ used in documentation; it's not valid code, since
the `bar' method must be called for an instance of the `Foo' class:
foo = [1, 2, 3] # an instance of Array
foo.reverse # invocation of Array#reverse
Thx for the answers. However, it's still not clear in my mind. Let's forget
about the period (.) for now.
Please let me know if I've got the following right:
1- Math::PI => means constant PI is defined in the Math module (code)
2- Proc::new => means new is a class method invoked on the Proc class
(code)
3- Kernel#lambda => expression meaning method lambda is defined in module
Kernel (documentation)
Last, can a method be also considered a constant? (which would explain
Proc::new, for example). I've seen somewhere (but can't remember where)
that Ruby methods can be seen as constants.
Thanks in advance.
···
On Thu, Apr 14, 2016 at 6:39 AM, stomar <sto.mar@web.de> wrote:
3- Kernel#lambda => expression meaning method lambda is defined in module
Kernel (documentation)
Yep. Although note that #lambda is private, so it can't be invoked with a
dot. But because it's defined in Kernel, which is universal, it's directly
available to every object (including the Main object.)
~~~
$ ruby -e 'p self; p lambda {}'
main
#<Proc:0x007f7359d5d490@-e:1 (lambda)>
$
~~~
Last, can a method be also considered a constant? (which would explain
Proc::new, for example). I've seen somewhere (but can't remember where)
that Ruby methods can be seen as constants.
I wouldn't think of them that way, as they can be mutated and even
undefined.
~~~
irb(main):001:0> class A; def b() "b"; end; end
irb(main):002:0> A.new.b
=> "b"
irb(main):003:0> class A; undef :b; end
irb(main):004:0> A.new.b
NoMethodError: undefined method `b' for #<A:0x007f5d23c507d8>
from (irb):4
from /usr/local/bin/irb2.3:11:in `<main>'
~~~
That said, you can invoke them using the scope resolution `::` thingy, as
discussed above.
1- both in code and documentation
2- both in code and documentation; Proc.new is also possible (for both)
3- `#' is used to refer to instance methods (_only_ in documentation)
e.g. in documentation you will see:
Array#reverse: the instance method `reverse' of the Array class
Array::new or Array.new: the class method `new' of the Array class
Regards,
Marcus
···
Am 15.04.2016 um 02:30 schrieb Marc Chanliau:
Matthew, Stomar,
Thx for the answers. However, it's still not clear in my mind. Let's
forget about the period (.) for now.
Please let me know if I've got the following right:
1- Math::PI => means constant PI is defined in the Math module (code)
2- Proc::new => means new is a class method invoked on the Proc class
(code)
3- Kernel#lambda => expression meaning method lambda is defined in
module Kernel (documentation)