Ruby is an object-oriented scripting language with features like mixins, metaprogramming, and dynamic typing. It allows defining functions and methods that can take blocks or lambdas as arguments. Arrays, hashes, and ranges provide ways to iterate over collections. Strings support interpolation and methods like split. Classes define methods that can access fields and local variables. The JSON library extends core types like Hash to add methods like to_json for serialization.
6. Arrays, hashes, ranges
# List of string, float, string, int and hash
array = ["Hello", 3.14, "ruby", 1, {hello: "hi"}]
for i in array
puts i
end
>> Hello
>> 3.14
>> ruby
>> 1
>> {:hello=>"hi"}
7. Arrays, hashes, ranges
for i in 1..10
puts i
end
for i in 'a'..'z'
puts i
end
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y, z
8. Arrays, hashes, ranges
# split(¡° ¡°)
puts %w(I still love you Kotlin)
"I",
"still",
"love",
"you",
"Kotlin"
9. String interpolation
a = 1
b = 4
puts "The number #{a} is less than #{b}"
puts "%0.5d" % 5
>> "The number 1 is less than 4"
>> "00005"
10. Blocks (lambdas)
5.times do |x|
puts "x inside the block: #{x}"
end
('a'..'z').each {|i| puts i}
>> x inside the block: 0
>> x inside the block: 1
>> x inside the block: 2
>> x inside the block: 3
>> x inside the block: 4
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y, z