Skip to content

Instantly share code, notes, and snippets.

@mvidner
Last active December 22, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvidner/6451197 to your computer and use it in GitHub Desktop.
Save mvidner/6451197 to your computer and use it in GitHub Desktop.
Ruby: .method vs ::constant
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>'
@mvidner
Copy link
Author

mvidner commented Sep 5, 2013

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 ..

@jreidinger
Copy link

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 😉

@mvidner
Copy link
Author

mvidner commented Sep 6, 2013

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