Last active
December 16, 2015 18:00
-
-
Save jamonholmgren/5474915 to your computer and use it in GitHub Desktop.
RubyMotion - using method_missing to automatically snake_case Obj-C methods.
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
class NSObject | |
def method_missing(meth, *args) | |
obj_c_meth = meth.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join | |
if respond_to?(obj_c_meth) | |
send obj_c_meth, *args | |
else | |
raise NoMethodError.new(meth.to_s) | |
end | |
end | |
end | |
# In the REPL: | |
v = UIView.new | |
# => #<UIView:0xaa45260> | |
v.background_color = UIColor.white_color | |
# => #<UICachedDeviceWhiteColor:0xaa41180> | |
v.backgroundColor | |
# => <UICachedDeviceWhiteColor:0x967c2e0> | |
v.asdf | |
# => #<NoMethodError: asdf> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment