Skip to content

Instantly share code, notes, and snippets.

@ooharak
Created December 7, 2012 07:42
Show Gist options
  • Save ooharak/4231539 to your computer and use it in GitHub Desktop.
Save ooharak/4231539 to your computer and use it in GitHub Desktop.
method chain feature for a Java object in JRuby
#method chain feature for a Java object in JRuby
class MethodChain
def initialize(underlying)
@underlying = underlying
end
def method_missing(name, *args, &block)
@underlying.send(name, *args, &block)
return self
end
def apply(&block)
yield self
end
def get
return @underlying
end
end
def example
require 'java'
frame = MethodChain.new(JavaxSwing::JFrame.new).
addKeyListener{|e| JavaLang::System::exit(0) if e.key_code == 033}.
apply{|f| f.contentPane.add JavaxSwing::JScrollPane.new('abc')}.
setPreferredSize(JavaAwt::Dimension.new(800,600)).
pack.
get
return frame
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment