Created
July 21, 2014 06:15
-
-
Save tjmcewan/5fd8a5185d86ddc33b18 to your computer and use it in GitHub Desktop.
putting alias_method above module_function allows the alias to be called
This file contains hidden or 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
~$ ruby -v *[master] | |
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0] | |
~$ irb *[master] | |
irb(main):001:0> module I18nHelper | |
irb(main):002:1> def custom_translate | |
irb(main):003:2> "boo" | |
irb(main):004:2> end | |
irb(main):005:1> | |
irb(main):006:1* module_function :custom_translate | |
irb(main):007:1> alias_method :ct, :custom_translate | |
irb(main):008:1> end | |
=> I18nHelper | |
irb(main):009:0> | |
irb(main):010:0* class TestI18nHelper | |
irb(main):011:1> include I18nHelper | |
irb(main):012:1> end | |
=> TestI18nHelper | |
irb(main):013:0> | |
irb(main):014:0* TestI18nHelper.new.custom_translate | |
NoMethodError: private method 'custom_translate' called for #<TestI18nHelper:0x007f9f528f7cc8> | |
from (irb):14 | |
from /Users/tim/.rubies/ruby-2.1.2/bin/irb:11:in '<main>' | |
irb(main):015:0> TestI18nHelper.new.ct | |
NoMethodError: private method 'ct' called for #<TestI18nHelper:0x007f9f528e4ad8> | |
from (irb):15 | |
from /Users/tim/.rubies/ruby-2.1.2/bin/irb:11:in '<main>' | |
irb(main):016:0> module I18nHelper | |
irb(main):017:1> def custom_translate | |
irb(main):018:2> "boo" | |
irb(main):019:2> end | |
irb(main):020:1> | |
irb(main):021:1* # now with alias_method first... | |
irb(main):022:1* alias_method :ct, :custom_translate | |
irb(main):023:1> module_function :custom_translate | |
irb(main):024:1> end | |
=> I18nHelper | |
irb(main):025:0> | |
irb(main):026:0* class TestI18nHelper | |
irb(main):027:1> include I18nHelper | |
irb(main):028:1> end | |
=> TestI18nHelper | |
irb(main):029:0> | |
irb(main):030:0* TestI18nHelper.new.custom_translate | |
NoMethodError: private method 'custom_translate' called for #<TestI18nHelper:0x007f9f52836fa0> | |
from (irb):30 | |
from /Users/tim/.rubies/ruby-2.1.2/bin/irb:11:in '<main>' | |
irb(main):031:0> TestI18nHelper.new.ct | |
=> "boo" | |
irb(main):032:0> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
as @vowel_boy notes,
alias_method
copies the method as it stands. so if it comes aftermodule_function
(which makes the method available to the module, but makes it private in any class it's included into), the private status gets copied also.