Skip to content

Instantly share code, notes, and snippets.

@jamonholmgren
Last active December 16, 2015 18:00
Show Gist options
  • Save jamonholmgren/5474915 to your computer and use it in GitHub Desktop.
Save jamonholmgren/5474915 to your computer and use it in GitHub Desktop.
RubyMotion - using method_missing to automatically snake_case Obj-C methods.
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