GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Monday, December 20, 2010

Method In Ruby

To say mooooo:
>> def sayMoo
>> puts 'Mooooooooooo...'
>> end

To call it, just type:
>> sayMoo

Method with parameter:
>> def sayMoo num
>> puts 'Mooooooooo...'*num
>> end

Call it by
>> sayMoo 5        # print 5 times
Share/Bookmark

Classes In Ruby

You creating class by:

>> class Satu
>>     def hello
>>         puts 'hello for you'
>>     end
>> end

Then you instantiate an object, write:
>> data = Satu.new
>> data.hello

Other example for creating an array:
>> class Satu
>>     def initialize name    # constructor
>>         @name = name    # @ is the class data
>>     end
>>     def setName name
>>         @name = name
>>     end
>>     def getName
>>         @name
>>     end
>> end

To instantiate an object, use:
>> data = Satu.new 'lady gaga'
>> puts data.getName    # print out 'lady gaga'
>> data.setName 'luna maya'  # 'luna maya' for getName
Share/Bookmark

Hash In Ruby

Hash in Ruby is like map in Java:

You can instantiate it like below:
data = {}
or
data = Hash.new

Then you insert an element toward it like below:
data['lady'] = 'gaga'
data['luna'] = 'maya'
data['aura'] = 'kasih'

To iterate to all of the hash, you need to type:
data.each do |key, value|
puts key +': '+value
end
Share/Bookmark

Array In Ruby

To define an array, use:
>> data = []
>> data[0] = 'lady gaga'
>> data[1] = 24
>> data[2] = 'new york'

or
>> data = ['lady gaga', 24, 'new york']
or
>> datas = [
>>       ['lady gaga','new york'],
>>       ['aura kasih','jakarta'],
>>       ['bill clinton','virginia']
>>     ]

To access an element, use the index:
>> puts data[0]     # lady gaga
>> puts datas[1][0]   # aura kasih

To iterate through all elements in the array, use:
>> data.each do |value|
>> puts value
>> end

To repeat the same thing over some times, use:
>> 3.times do
>> puts 'Holla World'
>> end
    
Other methods:
>> data.push 'barack obama'
>> data.pop            # remove and return the last element
>> data.length
>> data.last               # retrieve the last element
Share/Bookmark

While Loop In Ruby

while looping

>> comment = ''
>> while comment!='bye'
>> puts comment
>> comment = gets.chomp
>> end


Code to iterate:
>> i = 0;
>> while i<=10
>> puts i
>> i = i+1   # cannot use i++
>> end
Share/Bookmark

Flow Control In Ruby

Simple expression in comparison:
>> puts 2<1         // result false
>> puts 2!=8       // result true

Use if:
>> if (9>8)
>> puts '9 is greater than 8'
>> puts 'Hope you happy' 
>> else
>> puts '9 is not greater than 8'
>> end

Share/Bookmark

Friday, December 17, 2010

Ruby - Basic

To prompt output in colsole of Ruby, use:
>>> puts 'hello world!'    // single or double quotes is same
>> hello world

String aritmathics:
>>> puts 'one '+'day'
>> one day
>>> puts 'lol '*5
>> lol lol lol lol lol

To convert from number to string, use:
>>> id = 45
>>> puts id.to_s + ' indonesia raya'
>> 45 indonesia raya


To convert from string to number, use:
>>> code = '45'
>>> puts code.to_i + 5
>> 50

To convert from string to float, use:
>>> code = '45.3'
>>> puts code.to_f + 0.7
>> 46.0

Get input from keyboard:
>>> puts gets
>> holla for you all
>>> holla for you all

Binding keyboard input to variable:
>>> name = gets
>> sergey brin
>>> puts 'Your name is: '+ name +' and it's okey'
>> Your name is: sergey brin
>>  and it's okey


Removing CR-LF from gets
>>> name = gets.chomp
>> sergey brin
>>> puts 'Your name is: '+name+' and it's okey'
>> Your name is: sergey brin and it's okey

# sign is used for inline comment 

Share/Bookmark