Last active
August 29, 2015 14:18
-
-
Save darinwilson/d64bc5dd01a0447d0e93 to your computer and use it in GitHub Desktop.
Trying to open an Android class in RubyMotion
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
# 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 |
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
# 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 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
# 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