Ruby Introduction

Ruby Introduction



This introduction is not recommended to be read by you, you're derived from economics, law, politics, agriculture, forestry, design, civil engineering, public relations, religion, psychology, medicine, let alone literary, and others of his friends . It is recommended to be read by the children FASILKOM and his friends. This is because the content contained on this introduction very many shows FASILKOM terms once, and could understand only in, and can be described, by those who has an associate with the monitor for at least 1 semester fervently.


The meaning of Ruby in his dictionary Microsoft ® Encarta:


1. Gemstone red: a red precious stone that is a form of corundum. Use: jewelry, manufacture of watches, precision instruments.
2. deep red: Purplish glowing a deep red color like that of a ruby

So Ruby literally, etymologically, or whatever, means a red precious stones, precious jewels, shining rays.

So the importance of what I describe here about Ruby? precious stones like sales? (up important not important)


Ruby is a programming language that is quite popular in Japan today. Are also people who make gitu loh Japan. Yukihiro Matsumoto, a.k.a. "Matz''(It's time for Asian people to conquer the world!). Ruby is a scripting language (like shell scripts, perl, python), but also a real programming language and Object Oriented once powerful and elegant syntax for writing highly. (im not pretend to compare them). (Btw do the same this precious red stone hell?)

Ruby is a short history began when a maniac Matz OOP language to suit looking for a scripting-based OOP reply, but he did not find it, there's just perl and python, that was exactly not a true OOP. And then he decided to create a new language (I swear masya4JJI can not create a new language) wrote more powerful than perl and more OOP than python. (halahhh.. python snakes, and then type perl precious or animal?)

the first to be friends with Ruby installation must be done first. For Windows users, you can download a One-Click Ruby Installer here

easy, live download, then double clik, so . To check whether udah bener to install with or not, just type this command at a command prompt: ruby-v

If using Linux, there is usually've packages of Linux distribution they will be, ie: to use the live ubuntu apt-get command, refer again to each reporsitory linux linux distribution, and for Mac OSX've at include.

So what happens when you fail to install? or your Internet connection to download blocked? or you do not have a computer and just play in internet shop? or you do not have the money to buy his CD in shop? or read it you find hard? hahaahaa ..

Try this one had ever http://tryruby.hobix.com of your site they will be able to immediately try deh Ruby, a short tutorial as well, "Ruby in your Browser"

Writing a variable in Ruby as PHP does not have defined Hereinafter typenya but recognized that depending on the input given. Just a simple ..

Tipe2 basis of variables in Ruby are: Numbers, Strings, Arrays, Hashes, Symbosl, Ranges and Regular Expressions

Numbers and Strings
Ok, let's play with Ruby, the Interactive Ruby. Open a command prompt and type: IRB
C:\Documents and Settings\galihadika>irb

irb(main):001:0> a = 10

=> 10

irb(main):002:0> b = 20

=> 20

irb(main):003:0> a+b

=> 30

irb(main):004:0>

irb(main):004:0> string = “Bermain - main dengan Ruby”

=> “Bermain - main dengan Ruby”

irb(main):005:0> string.length

=> 26

irb(main):006:0> string.reverse

=> “ybuR nagned niam - niamreB”

irb(main):007:0> string.index(”B”)

=> 0

Ruby is a true OOP language isn’t it ?
When a variable in a value then assign these variables to be in accordance with Inputan type. And tessebut variable is an object. You can call its method, like the example above string.length, string.index

Arrays dan Hashes
Still on the IRB (Interactive Ruby)
Writing an array of Ruby just simple like this:

irb(main):008:0> arraynih = [1,”aja-aja ada”, “yahuu”, “gugel”, 10]

=> [1, “aja-aja ada”, “yahuu”, “gugel”, 10]

irb(main):009:0> arraynih[1]

=> “aja-aja ada”

irb(main):010:0> arraynih.length

=> 5
To create an empty new Array :
irb(main):011:0> empty = []

=> []

irb(main):012:0> empty1 = Array.new

=> []

And again Array is an object, anda dapat memanggil method sperti length, sort, dll, baca doc nya Ruby.
Nil not Null !
irb(main):013:0> arraynih[3] = nil

=> nil

To create an index of the array value is empty, use nill not null! wat is it? nil is actually an object too. Ruby and recognize it, so there will not declare error "null pointer error" (tired clay java continued) hehehhehe
irb(main):014:0> string = %w{ satu dua tiga empat lima }

=> [”satu”, “dua”, “tiga”, “empat”, “lima”]

irb(main):015:0> string[3]

=> “empat”

irb(main):016:0> string[2]

=> “tiga”


Dizziness was missing from your brain, I believe that u usually have to write double quote and comma for each of the string element of your array. In Ruby just use% w, and the problem was not attached to pat on the head, very elegant is not it?


For the hash, consisting of key and value,
irb(main):027:0> hashSaya = {

irb(main):028:1* ’satu’ => ’satu’,

irb(main):029:1* ‘dua’ => ‘dua’,

irb(main):030:1* 3 => ‘tiga’,

irb(main):031:1* 4 => ‘empat’}

=> {”satu”=>”satu”, “dua”=>”dua”, 3=>”tiga”, 4=>”empat”}

irb(main):032:0> hashSaya[’satu’]

=> “satu”

irb(main):033:0> hashSaya[3]

=> “tiga”


Did you have cured a headache?

Symbol

Symbol is a representation of a string. To be honest I was still confused about the function and the fundamental differences of this symbol, but this symbol is usually used as the key of a hash.

Writing is as follows:
:symbol1

:symbol2

Implementation of the hash:
irb(main):034:0> hash2 = {:satu => 1, :dua=> 2}

=> {:dua=>2, :satu=>1}

irb(main):035:0> hash2[:satu]

=> 1

Just the same hash result as usual. This might be cool to be writing more and more organized .. so know this was a sign ":" (colon) means the key of the hash .

Ranges


In the Indonesian sense is a distance from something to something else.

Immediately, for example:
rb(main):036:0> rangesnih = 1..10

=> 1..10

irb(main):037:0> rangesnih.max

=> 10

irb(main):038:0> rangesnih.min

=> 1


Above example has a value ranges from 1 to 10, again these ranges is also an object (everything is an object). Call the method max and it returns the max value of the ranges

Can also be used for iteration:
irb(main):039:0> rangesnih.each do |i|

irb(main):040:1* puts i

irb(main):041:1> end

1

2

3

4

5

6

7

8

9

10

=> 1..10

Or, if you want to convert ranges into array :
irb(main):047:0> arr = rangesnih.to_a

=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

irb(main):048:0> arr[1]

=> 2

irb(main):049:0> arr1 = (’abc’..’abf’).to_a

=> [”abc”, “abd”, “abe”, “abf”]

irb(main):050:0> arr1[3]

=> “abf”

Regular Expression


Used to perform a set pattern matching with a particular string.
Writing can be done by:
irb(main):051:0> a = Regexp.new(’^\s*[a-z]’)

=> /^\s*[a-z]/

irb(main):052:0> b = /^\s*[a-z]/

=> /^\s*[a-z]/

irb(main):053:0> c = %r{^s*[a-z]}

=> /^s*[a-z]/

So there r three ways to initialize Regular Expression

1. Regexexp.new(‘pattern’)

2. /pattern/

3. %r{pattern}

Pattern matching :
irb(main):054:0> a = “test doang”

=> “test doang”

irb(main):055:0> a =~ /o/

=> 6

irb(main):056:0> a !~ /o/

=> false

Operator "= ~" is used to compare the pattern with the string if it is true, if true then the return position in the string.

While the operator "~" states are not equal to the pattern, the example above produce false because the "o" is the string.

Struktur Kontrol

Selection
If condition

Action

Elseif condition1

Action1

Else

ActionElese

end


This period also requested described!! (too much ..)
case [target]

when [comparison1]

action1

when [comparison2]

action2

[ else

body ]

end

[target] is the target variable selection

[comparison] is the value given for comparison with the target variable if true then run.

[else] is used as the default action if no reply from the comparison true

Perulangan


Use for:
for [varname] in [collection] do

action

end

[varname] is a variable name that will be used as the value of each collection

[collection] is a set of values that will iterate until the completion of the for

Example:
irb(main):065:0> a = 1..10

=> 1..10

irb(main):066:0> for i in a do

irb(main):067:1* puts i

irb(main):068:1> end

1

2

3

4

5

6

7

8

9

10

=> 1..10

Using the while-until:

while expression do

action

end

until expression do

action

end


differences of the two models above is repeated, while doing looping for expression is true, whereas looping until done during the expression value false.

In the iteration if there is usually a break and continue, then there is a break in Ruby and next, the same function.