Last active
December 22, 2015 09:19
-
-
Save mvidner/6451197 to your computer and use it in GitHub Desktop.
Ruby: .method vs ::constant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mvidner@mrakoplas:~$ irb | |
irb(main):001:0> require 'yast' | |
<internal:prelude>:18: warning: already initialized constant MUTEX_FOR_THREAD_EXCLUSIVE | |
=> true | |
irb(main):003:0> Yast.import "Label" | |
=> nil | |
irb(main):006:0> Yast::Label.AddButton | |
=> "&Add" | |
irb(main):007:0> Yast::Label::AddButton | |
TypeError: #<Yast::LabelClass:0x00000002742000 @my_textdomain="base"> is not a class/module | |
from (irb):7 | |
from /usr/bin/irb:12:in '<main>' | |
irb(main):008:0> Yast::BACKTRACE_REGEXP | |
=> /^(.*):(\d+):in `.*'$/ | |
irb(main):009:0> Yast.BACKTRACE_REGEXP | |
NoMethodError: undefined method 'BACKTRACE_REGEXP' for Yast:Module | |
from (irb):9 | |
from /usr/bin/irb:12:in '<main>' |
That is exactly what I write in my comment. So probably just misunderstanding.
[reformatted] But you are right that in this case it doesn't happen, because there is LanItems.GetCurrentName and for constant it must be LanItems::GetCurrentName . but if it stand alone, there must be () or use ruby convention 😉
Ah, I indeed misunderstood. BTW, while here we distinguish between constants and methods, I have another snippet that compares constants to variables: https://gist.github.com/mvidner/4002448
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The question is whether you must use empty parentheses when calling a method that looks like a constant (that is, begins with a capital letter). I think this gist demonstrates that it is not true. At time of use, constant-vs-method is decided by the delimiter,
::
vs.
.