Sources: https://www.ruby-lang.org/en/, 1,
RUBY : A 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.
Installation :
OR,
#statement separator :
; Or new line
#Comment
#is the comment line and will be ignored by ruby interpreter
#variable assignment-
a="This is a test. "
b=10
c = "Some text"
#print/display
puts "Hello World"
#string placeholders is #{}.
puts "#{a} #{b}" #double Quotes is must.
#String Concatenation
puts a + c #is right as both are strings
puts a + b #is wrong and results in error (TypeError: can't convert Fixnum into String) as b is integer & a is string
#For Loop
for i in 0..10
puts "Hello #{i}"
end
#play with array in irb
[4,5,2,9,1].max
=> 9
-------------------------------------------
[4,5,2,9,1].min
=> 1
-------------------------------------------
[4,5,2,9,1].sort
=> [1, 2, 4, 5, 9]
-------------------------------------------
[4,5,2,9,1].reverse
=> [1, 9, 2, 5, 4]
-------------------------------------------
[4,5,2,9,1].sort.reverse
=> [9, 5, 4, 2, 1]
-------------------------------------------
for i in [4,5,2,9,1]
puts i * i
end
16
25
4
81
1
=> [4, 5, 2, 9, 1]
-------------------------------------------
#calculate in irb
1+2+3+4+5
=> 15
1+2*3
=> 7
1+2*3/3
=> 3
1+2*3/3+(5-4)
=> 4
Example: ruby_example_1.7z