Skip to content

Instantly share code, notes, and snippets.

@darinwilson
Last active August 29, 2015 14:18
Show Gist options
  • Save darinwilson/d64bc5dd01a0447d0e93 to your computer and use it in GitHub Desktop.
Save darinwilson/d64bc5dd01a0447d0e93 to your computer and use it in GitHub Desktop.
Trying to open an Android class in RubyMotion
# throws NameError: uninitialized constant `Android'
class Android::App::Activity
def say_hello
puts "hello!"
end
end
class MainActivity < Android::App::Activity
def onCreate(savedInstanceState)
super
say_hello
end
end
# throws com.rubymotion.NoMethodError: undefined method `say_hello' for #<MainActivity:0xbee810bc>
module Android
module App
class Activity
def say_hello
puts "hello!"
end
end
end
end
class MainActivity < Android::App::Activity
def onCreate(savedInstanceState)
super
say_hello
end
end
# This is another variation on the problem: monkey patching a Ruby class works fine, but
# the patch is not picked up by the Java version of the same class (e.g. Hash and
# java.util.HashMap). This will be needed when working with 3rd party Java libraries that
# will be returning objects into RubyLand, e.g. Volley
class Hash
def say_hello
puts "hello"
end
end
class MainActivity < Android::App::Activity
def onCreate(savedInstanceState)
super
# this works
h = {}
h.say_hello
# this raises: undefined method `say_hello' for {}:java.util.HashMap
hash = Java::Util::HashMap.new
hash.say_hello
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment