Tuesday, February 23, 2010

Array Pada Ruby

Kemudahan pemrosesan array pada Ruby merupakan salah satu kelebihan bahasa pemrograman Ruby. Untuk informasi lebih lanjut mengenai array bisa dilihat di http://ruby-doc.org
#!/usr/bin/env ruby

# Set Intersection
my_a = [ 1, 1, 5, 3 ] & [ 1, 3, 7 ]
p my_a #=> [ 1, 3 ]

# penggabungan array / Concatenation
b = [ 1, 2, 3 ] + [ 4, 5 ]
p my_b #=> [ 1, 2, 3, 4, 5 ]

# Array Difference
my_c = [ 1, 1, 2, 2, 3, 4, 4, 5 ] - [ 1, 2, 3 ]
p my_c #=>  [ 4, 4, 5 ]

# Himpunan / Set Union
my_d = [ "a", "b", "c" ] | [ "c", "d", "a" ] 
p my_d #=> [ "a", "b", "c", "d" ]

# Menambahkan elemen ke dalam array / append element
my_array = [ 1, 2 ]
my_array << "c"
my_array << "d"
my_array << [ 3, 4 ]
my_array << 7 << 8
p my_array #=>[1, 2, "c", "d", [3, 4], 7, 8]



No comments:

Post a Comment

© Railsmine