Learning Ruby: Syntax (Part 2)

Learning continues …

(1..5).each do |counter|
  puts "iteration #{counter}"
end

Although python like loop also work

for counter in 1..5
  puts "iteration #{counter}"
end

There should be one– and preferably only one –obvious way to do it.

e.g. a.map do .. end, a.map { |el| e.something } and a.map(&:method)

➜ cat test.rb
array = [1,2,3,4,5]
sqr = array.map do |item|
    item * item
end
puts sqr

a = ["Foo", "bAr", "baZ"]
puts a.map { |s| s.downcase }
puts a.map(&:upcase)


mandar in /tmp via 💎 v3.1.0
➜ ruby test.rb
1
4
9
16
25
foo
bar
baz
FOO
BAR
BAZ
Python Ruby
try begin/end
except rescue
else else
ensure finally