This page appliesonly to version1.9.2 ofRuby on Win Intel x86 / Linux AMD64 Platforms.
This page illustrates the Ruby Code Examples, in a straight way than to lengthy explanations, this can be used like an immediate reference for both syntax and programming ideas. Concise methods attempting a problem with a pragmatic approach are also discussed.
puts'Hi!'# puts the string to stdoutprint'ram:'# print does not terminate with default \n at the end of executionname=gets.chomp# read from stdinputs"Hi!#{name}"# interpolates the string, replaces name with its value
print'c:\books\net\apps\tools'# outputs c:\books\net\apps\toolsprint"c:\books"# outputs books
String interpolation allows a variable to get replaced by its value while execution
One=1puts"#{One} is a number"# outputs, 1 is a numberputs"%d is a number"%One# outputs, 1 is a number
%Q and %q are string literals that are with special properties
puts%Q^ is Quote friendly^puts%q# you can type "Quotes"!! ## outputs, you can type "Quotes"!!
Multiline Strings are defined with '<<' prefix to a named delimiter, like
puts<<XYZNormally programmers try to code to solve the problem and forgets once it is done,but some explore more, to find a concise opproach to the problem,and thats makes him !(repetative)XYZ
Ruby is really flexible with strings
# choose the one you like' this '" this "%/ this /%q{ this }%Q^ this ^# value => " this "
The string delimiter pairs are '', "", but with %, %q and %Q prefix, we have //, {}, ##, as delimiters, the unofficial rule is to use the delimiter that is not used to declare the string itself, this is to declareraw strings to avoiddelimiter collision
puts%/ '' "" {} ## ^^ /# outputs, '' "" {} ## ^^
An Object is something that can be handled with defined Methods, even for the physical objects around us there are defined methods in using them, whenever we say 'object' it is to mean that it has some methods through which the object can be handled
-1.abs# => 1'1234567'.length# => 71234567.to_s.length# => 73.times{print3}# outputs, 333rand(10).times{|x|putsx}# => random set of numbers from 0 to 10
Ruby is object oriented, everything is an object including numbers, strings and even thenil
1.class# => Fixnum1.0.class# => Float'xyz'.class# => Stringnil.class# => NilClass
Each of these can use theeach method for iteration, though they seems to be synonyms, there is lot of difference in computing, TheEnumerators and Ranges are almost of fixed memory length and so are memory efficient and fast, whereasArrays andHashes involves complexdata structures for to be dynamic
5.times.class# => Enumerator5.upto(10).class# => Enumerator5.upto(10).next# => 5
(0...10).class# => Range(0..9).class# => Range(0..2).first# => 0(0..2).last# => 2(1..5).next# invalid, Range class doesent have next method(0..3).each{|x|printx}# outputs, 0123(0...10).reverse_each{|x|printx}# outputs, 9876543210(-3..3).each.abs{|x|printx}# invalid(-3..3).each{|x|printx.abs}# outputs, 3210123# Enumerator doesn't require 'each' to iterate5.upto(10).class# => Enumerator5.upto(10){|x|printx}# outputs, 5678910(5..10).each{|x|printx}# outputs, 5678910
Anarray is a collection of elements, by default an array ofn elements has itsindex enumerated from0 ton-1, i.e, the index to the first element of an array is0
a=[4,6,7,5]# simple array declarationa.length# => 4a.rotate# => [6, 7, 5, 4]a.sort# => [4, 5, 6, 7]a.sort.reverse# => [7, 6, 5, 4]a[0]# => 4a[3]# => 5a[4]=3# => 3 ;resulting array is [4, 6, 7, 5, 3]a<<1# => [4, 6, 7, 5, 3, 1] ; useful when array size is unknowna[10]=0# => 0 ;resulting array is [4, 6, 7, 5, 3, 1, nil, nil, nil, nil, 0]a.length# => 11
array of arrays, array of ranges and arrays of mixeddatatypes are allowed to declare,Object Oriented!
hex=[(0..9),('A'..'F')]hex.each{|x|x.each{|y|printy}}# outputs, 0123456789ABCDEF# declare an array of arraysnums=[[0,1],[2,3,4,5,6,7],[8,9],['A','B','C','D','E','F']]binary=nums[0]# => [0, 1]octal=nums[0]+nums[1]# => [0, 1, 2, 3, 4, 5, 6, 7]decimal=nums[0]+nums[1]+nums[2]# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]hexadecimal=nums.flatten# => [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F']octal=(binary+octal).uniq# => [0, 1, 2, 3, 4, 5, 6, 7]a=[0,1,2,3,4,5]# array of 6 elementsb=a.map{|x|2**x}# => [1, 2, 4, 8, 16, 32]
Hash is anassociative array which is a collections ofKeys and it's associatedValues
capitals={:france=>'Paris',:England=>'London'}capitals[:westbengal]='Kolkata'# append a new elementcapitals[:karnataka]='Bengaluru'# change an element's association
aliaschantprint108.times{chant'Hare Krishna!'}# see the change for yourself
defiteratoryield'yield, 'yield'blocks,'yield'Ruby'enditerator{|yeilded|print"use#{yeilded}"}# outputs, use yield, use blocks, use Ruby