1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-14 02:34:17 +02:00

Merge pull request #1392 from ghalley/updates

[ruby/en] Add modulus, array.first, array.last, and each_with_index
This commit is contained in:
Levi Bostian
2015-10-12 23:00:09 -05:00

View File

@@ -12,6 +12,7 @@ contributors:
- ["Dzianis Dashkevich", "https://github.com/dskecse"] - ["Dzianis Dashkevich", "https://github.com/dskecse"]
- ["Levi Bostian", "https://github.com/levibostian"] - ["Levi Bostian", "https://github.com/levibostian"]
- ["Rahil Momin", "https://github.com/iamrahil"] - ["Rahil Momin", "https://github.com/iamrahil"]
- ["Gabriel Halley", https://github.com/ghalley]
--- ---
@@ -39,6 +40,7 @@ You shouldn't either
10 * 2 #=> 20 10 * 2 #=> 20
35 / 5 #=> 7 35 / 5 #=> 7
2**5 #=> 32 2**5 #=> 32
5 % 3 #=> 2
# Arithmetic is just syntactic sugar # Arithmetic is just syntactic sugar
# for calling a method on an object # for calling a method on an object
@@ -160,6 +162,7 @@ array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]
# Arrays can be indexed # Arrays can be indexed
# From the front # From the front
array[0] #=> 1 array[0] #=> 1
array.first #=> 1
array[12] #=> nil array[12] #=> nil
# Like arithmetic, [var] access # Like arithmetic, [var] access
@@ -170,6 +173,7 @@ array.[] 12 #=> nil
# From the end # From the end
array[-1] #=> 5 array[-1] #=> 5
array.last #=> 5
# With a start index and length # With a start index and length
array[2, 3] #=> [3, 4, 5] array[2, 3] #=> [3, 4, 5]
@@ -264,6 +268,12 @@ hash.each do |key, value|
puts "#{key} is #{value}" puts "#{key} is #{value}"
end end
# If you still need and index you can use "each_with_index" and define an index
# variable
array.each_with_index do |element, index|
puts "#{element} is number #{index} in the array"
end
counter = 1 counter = 1
while counter <= 5 do while counter <= 5 do
puts "iteration #{counter}" puts "iteration #{counter}"