#Ruby Basics ##Home ##Overview
- Bahasa pengaturcaraan Berorientasikan Objek ‘Object Oriented’ di bawah Sumber Terbuka ( Open Source )
- Bahasa Pengaturcaraan berasaskan skrip ( scripting language )
- Dicipta oleh Yukihiro Matsumoto pada tahun 1995 !
- Digunakan bukan sahaja untuk Applikasi Web. Cth : Google Sketchup macro scripting API, dll. Dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. from http:// www.ruby lang.com
##Environment
Sila layari laman web http://tryruby.org untuk mencuba ruby secara ‘online’.
##Syntax
Marilah kita menulis program yang mudah dalam ruby. Semua fail ruby akan mempunyai sambungan .rb. Jadi, letakkan kod sumber berikut dalam fail test.rb.
#!/usr/bin/ruby -w
puts "Hello, Ruby!";Sekarang, cuba untuk menjalankan program ini seperti berikut:
$ ruby test.rb
Ini akan menghasilkan keputusan seperti berikut:
Hello, Ruby!
Aksara ruang kosong seperti ruang dan tab biasanya diabaikan kod Ruby, kecuali apabila mereka muncul dalam string. Tafsiran seperti ini menghasilkan amaran apabila pilihan w dinyatakan.
a + b is interpreted as a+b ( Here a is a local variable)
a +b is interpreted as a(+b) ( Here a is a method call)
###Line Endings dalam Program Ruby:
Ruby menafsirkan koma bertitik (;) dan newline character sebagai pengakhiran satu kenyataan. Walau bagaimanapun, jika Ruby menghadapi operator, seperti +, -, atau 'backslash'() di akhir garisan, mereka menunjukkan penerusan satu kenyataan.
###Ruby Identifier
Pengenal pasti adalah nama-nama pembolehubah, pemalar, dan kaedah. Ruby pengenalan adalah sensitif huruf. Ia bermakna Ram dan RAM dua idendifiers berbeza dalam Ruby.
Ruby nama pengecam boleh terdiri daripada huruf dan aksara garis bawah (_).
Perkataan Cipta Terpelihara: Senarai berikut menunjukkan perkataan reserved dalam Ruby. Ini terpelihara kata-kata tidak boleh digunakan sebagai nama malar atau berubah-ubah. Mereka boleh, bagaimanapun, digunakan sebagai nama kaedah.
| BEGIN | do | next | then |
| END | else | nil | true |
| alias | elsif | not | undef |
| and | end | or | unless |
| begin | ensure | redo | until |
| break | false | rescue | when |
| case | for | retry | while |
| class | if | return | yield |
| def | in | self | __FILE__ |
| defined? | module | super | __LINE__ |
##Classes
Penentuan Kelas di Ruby: Melaksanakan pengaturcaraan berorientasikan objek dengan menggunakan Ruby, anda perlu terlebih dahulu belajar bagaimana untuk mencipta objek dan kelas di Ruby.
Satu kelas di Ruby selalu bermula dengan 'class' kata kunci yang sejajar dengan nama kelas. Nama ini perlu sentiasa dalam huruf awal. Kelas Pelanggan boleh dipaparkan sebagai
class Customer
end
Anda menamatkan kelas dengan menggunakan 'end' kata kunci. Semua ahli data dalam kelas adalah di antara definisi kelas dan kata kunci akhir.
##Variables
Pembolehubah dalam Kelas Ruby: Ruby menyediakan empat jenis pembolehubah:
Pembolehubah tempatan: pembolehubah tempatan pembolehubah yang ditakrifkan dalam kaedah. pembolehubah tempatan tidak boleh didapati di luar kaedah. Anda akan melihat lebih banyak butiran mengenai kaedah dalam bab berikutnya. pembolehubah tempatan bermula dengan huruf kecil atau _.
Pembolehubah instance: Pembolehubah instance kaedah seluruh tersedia untuk apa-apa keadaan atau objek tertentu. Ini bermakna bahawa pembolehubah contoh perubahan dari objek untuk membantah. pembolehubah contoh didahului oleh pada tanda (@) diikuti dengan nama yang berubah-ubah.
#!/usr/bin/ruby
class Customer
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
end
# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
# Call Methods
cust1.display_details()
cust2.display_details()
Pembolehubah Kelas: pembolehubah kelas boleh didapati di seluruh objek yang berbeza. Pemboleh ubah kelas tergolong dalam kelas dan merupakan ciri-ciri kelas. Mereka didahului oleh tanda @@ dan diikuti oleh nama yang berubah-ubah.
#!/usr/bin/ruby
class Customer
@@no_of_customers=0
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
@@no_of_customers += 1
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
def total_no_of_customers()
puts "Total number of customers: #@@no_of_customers"
end
end
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
cust1.total_no_of_customers() cust2.total_no_of_customers()
Pembolehubah Global: pembolehubah Kelas tidak tersedia di seluruh kelas. Jika anda ingin mempunyai pembolehubah tunggal, yang boleh didapati di seluruh kelas, anda perlu menentukan pembolehubah global. Pembolehubah global sentiasa didahului dengan tanda dolar ($).
#!/usr/bin/ruby
$global_variable = 10
class Class1
def print_global
puts "Global variable in Class1 is #$global_variable"
end
end
class Class2
def print_global
puts "Global variable in Class2 is #$global_variable"
end
end
class1obj = Class1.new
class1obj.print_global
class2obj = Class2.new
class2obj.print_global
##Operators
###Operator aritmetik Ruby:
| Operator | Description | Example |
|---|---|---|
| + | Addition - Adds values on either side of the operator | a + b will give 30 |
| - | Subtraction - Subtracts right hand operand from left hand operand | a - b will give -10 |
| * | Multiplication - Multiplies values on either side of the operator | a * b will give 200 |
| / | Division - Divides left hand operand by right hand operand | b / a will give 2 |
| % | Modulus - Divides left hand operand by right hand operand and returns remainder | b % a will give 0 |
| ** | Exponent - Performs exponential (power) calculation on operators | a**b will give 10 to the power 20 |
###Operators Perbandingan Ruby:
Andaikan pembolehubah memegang 10 dan b berubah memegang 20, maka:
| Operator | Description | Example |
|---|---|---|
| == | Checks if the value of two operands are equal or not, if yes then condition becomes true. | (a == b) is not true. |
| != | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. | (a != b) is true. |
| > | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (a > b) is not true. |
| < | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (a < b) is true. |
| >= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (a >= b) is not true. |
| <= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (a <= b) is true. |
| <=> | Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second. | (a <=> b) returns -1. |
| === | Used to test equality within a when clause of a case statement. | (1...10) === 5 returns true. |
| .eql? | True if the receiver and argument have both the same type and equal values. | 1 == 1.0 returns true, but 1.eql?(1.0) is false. |
| equal? | True if the receiver and argument have the same object id. | if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true. |
###Operators Tugasan Ruby:
Andaikan pembolehubah memegang 10 dan b berubah memegang 20, maka:
| Operator | Description | Example |
|---|---|---|
| = | Simple assignment operator, Assigns values from right side operands to left side operand | c = a + b will assigne value of a + b into c |
| += | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | c += a is equivalent to c = c + a |
| -= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | c -= a is equivalent to c = c - a |
| *= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | c *= a is equivalent to c = c * a |
| /= | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand | c /= a is equivalent to c = c / a |
| %= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | c %= a is equivalent to c = c % a |
| **= | Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand | c **= a is equivalent to c = c ** a |
##Comments
Anda boleh memberi komen banyak baris menggunakan = bermula dan sintaks = akhir seperti berikut:
#!/usr/bin/ruby -w
puts "Hello, Ruby!"
=begin
This is a multiline comment and con spwan as many lines as you
like. But =begin and =end should come in the first line only.
=end##IF...ELSE ##Loops ##Methods ##Blocks ##Modules ##Strings ##Arrays ##Hashes ##Date & Time ##Ranges ##Iterators ##File I/O ##Exceptions