Rouninja's Blog

A journey to become rubykage

Basic: Array

leave a comment »

Seringkali belajar dengan melihat( dan mempraktikkan) contoh itu membuat saya (kita?) lebih mudah memahami suatu konsep. Agree? :D

Contoh 1:


a = [ 3.14159, "pie", 99 ]

a.class    # =>  Array

a.length   # =>  3

a[0]       # =>  3.14159

a[1]       # =>  "pie"

a[2]       # =>  99

a[3]       # =>  nil

Contoh 2:


b = Array.new

b.class    # => Array

b.length   # => 0

b[0] = "second"

b[1] = "array"

b          # => ["second", "array"]

Pada ruby, index array dimulai dari 0. Juga dikenal index dengan angka negatif, yg berarti index dihitung dari posisi paling belakang. Lihat contoh aja, biar lebih mudah dimengerti:

Contoh 3:


a = [ 1, 3, 5, 7, 9 ]

a[-1]    # =>  9

a[-2]    # =>  7

a[-99]   # =>  nil

array index

array index

Contoh 4 – mengambil beberapa elemen array:


a = [ 1, 3, 5, 7, 9 ]

a[1, 3]    # =>  [3, 5, 7]

a[3, 1]    # =>  [7]

a[-3, 2]   # =>  [5, 7]

Contoh 5 – range indexing:


a = [ 1, 3, 5, 7, 9 ]

a[1..3]     # =>  [3, 5, 7]

a[1...3]    # =>  [3, 5]

a[3..3]     # =>  [7]

a[-3..-1]   # =>  [5, 7, 9]

Contoh 6 – Mereplace elemen array


a = [ 1, 3, 5, 7, 9 ]  # =>   [1, 3, 5, 7, 9]

a[1] = ’bat’      # =>       [1, "bat", 5, 7, 9]

a[-3] = ’cat’     # =>       [1, "bat", "cat", 7, 9]

a[3] = [ 9, 8 ]   # =>       [1, "bat", "cat", [9, 8], 9]

a[6] = 99         # =>       [1, "bat", "cat", [9, 8], 9, nil, 99]

Contoh 7 – Mereplace / menambahkan elemen array


a = [ 1, 3, 5, 7, 9 ]    # =>         [1, 3, 5, 7, 9]

a[2, 2] = ’cat’          # =>         [1, 3, "cat", 9]

a[2, 0] = ’dog’          # =>         [1, 3, "dog", "cat", 9]

a[1, 1] = [ 9, 8, 7 ]    # =>         [1, 9, 8, 7, "dog", "cat", 9]

a[0..3] = []             # =>         ["dog", "cat", 9]

a[5..6] = 99, 98         # =>         ["dog", "cat", 9, nil, nil, 99, 98]

Contoh 8 – Beberapa array method


# method push untuk menambah elemen array.

# method pop untuk mengambil elemen array dari belakang

stack = []

stack.push "red"

stack.push "green"

stack.push "blue"

p stack                 # =>    ["red", "green", "blue"]

puts stack.pop          # =>    blue

puts stack.pop          # =>    green

puts stack.pop          # =>    red

p stack                 # =>    []

#method shift untuk mengambil elemen array dari depan

queue = []

queue.push "red"

queue.push "green"

p queue                # => ["red","green"]

puts queue.shift       # =>   red

puts queue.shift       # =>   green

p queue                # => []

array = [ 1, 2, 3, 4, 5, 6, 7 ]

p array.first(4)         # =>   [1, 2, 3, 4]

p array.last(4)          # =>   [4, 5, 6, 7]

p array                  # =>   [1, 2, 3, 4, 5, 6, 7]

Okey, saatnya buka irb dan….happy coding. :D

Advertisement

Written by Rounin

November 11, 2009 at 19:20

Posted in ruby

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.