Skip to content

Instantly share code, notes, and snippets.

@ajuckel
Created August 25, 2011 15:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ajuckel/1171017 to your computer and use it in GitHub Desktop.
Varargs Examples
Java:
import java.lang.StringBuilder;
public class Test {
public static String concat(String... strings) {
StringBuilder sb = new StringBuilder();
for( String s : strings ) {
sb.append(s);
}
return sb.toString();
}
}
jirb:
jruby-1.6.3 :001 > require 'java'
=> true
jruby-1.6.3 :002 > import 'Test'
=> Java::Default::Test
jruby-1.6.3 :003 > Test.concat("foo", "bar")
=> "foobar"
jruby-1.6.3 :004 > def concat(*args)
jruby-1.6.3 :005?> args.join("")
jruby-1.6.3 :006?> end
=> nil
jruby-1.6.3 :007 > concat("foo", "bar", "baz")
=> "foobarbaz"
jruby-1.6.3 :008 > arr = ["foo", "bar"]
=> ["foo", "bar"]
jruby-1.6.3 :009 > concat(*arr)
=> "foobar"
jruby-1.6.3 :010 > Test.concat(*arr)
=> "foobar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment