Skip to content

Instantly share code, notes, and snippets.

@woodie
Created July 6, 2010 02:55
Show Gist options
  • Save woodie/464939 to your computer and use it in GitHub Desktop.
Save woodie/464939 to your computer and use it in GitHub Desktop.
Working with int[] and String[]
# this is an array
$ duby -e "a = int[5]
a[0] = 3
a[1] = 4
a[4] = 8
puts a.each {|i| puts i}"
3
4
0
0
8
null
# this is a list
$ duby -e "a = [1,2,3,4]
> a.each {|i| puts i}"
1
2
3
4
# oops, that won't work
$ duby -e "a = [1,2,3,4,5,6,7]
a[2] = 5"
Inference Error:
DashE:2: Method []=(Type(int), Type(byte)) on Type(java.util.List) not found
# Arrays will sort int[] and Object[]
$ duby -e "import java.util.Arrays
> a = int[4]; a[0] = 5; a[1] = 9; a[2] = 0; a[3] = 6
> Arrays.sort(a)
> a.each {|i| puts i}"
0
5
6
9
# Arrays will not sort String[]
$ duby -e "import java.util.Arrays
a = 'this is so much fun'.split(' ')
Arrays.sort(a)
a.each {|i| puts i}"
Inference Error:
DashE:3: Method sort(Type(java.lang.String array)) on Type(java.util.Arrays meta) not found
# this is an array
$ duby -e "a = String[5]
a[0] = 'fun'
a[1] = 'for'
a[4] = 'all'
puts a.each {|i| puts i}"
fun
for
null
null
all
null
# this is an array
$ duby -e 'a = "this is lots of fun".split(" ")
a.each {|i| puts i}'
this
is
lots
of
fun
# hey, that works!
$ duby -e 'a = "this is lots of fun".split(" ")
a[4] = "trouble"
a.each {|i| puts i}'
this
is
lots
of
trouble
# this won't work
$ duby -e 'a = ["this", "is", "so", "much", "fun"]
a[4] = "trouble"
a.each {|i| puts i}'
Inference Error:
DashE:2: Method []=(Type(int), Type(java.lang.String)) on Type(java.util.List) not found
# this works
$ duby -e 'a = ["this", "is", "so", "much", "fun"]
puts a.get(0).kind_of?(String)'
true
# this works too
$ duby -e 'a = ["this", "is", "so", "much", "fun"]
puts a.get(3)'
much
$ duby -e 'a = ["this", "is", "so", "much", "fun"]
puts a.contains("fun")'
true
$ duby -e 'a = ["this", "is", "so", "much", "fun"]
puts a.contains("ugly")'
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment