GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

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

No comments:

Post a Comment