Created
April 12, 2018 09:47
-
-
Save barangerbenjamin/930b0094c12ff6af7d902feb165c98bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ONE LINE SYNTAX | |
{ |i| i * 2} | |
# MULTI-LINE SYNTAX | |
do |i| | |
i * 2 | |
end | |
# USE OF BLOCK WITHOUT PARAMETERS | |
def timer | |
start_time = Time.now | |
yield # EXECUTE THE BLOCK | |
end_time = Time.now | |
puts "Elpased time = #{end_time - start_time}" | |
end | |
timer do | |
puts "starting task 1" | |
sleep(2) | |
puts "Task 1 is done" | |
end | |
timer do | |
puts "starting task 2" | |
sleep(1) | |
puts "Task 2 is done" | |
end | |
# BLOCK CAN TAKE PARAMETERS | |
def greet(name, last_name) | |
puts yield(name, last_name) | |
end | |
greet("john", "doe") do |elephant, elephant_last_name| | |
"Greetings #{elephant.capitalize} #{elephant_last_name.capitalize}, you look confused today" | |
end | |
greet("john") do |name| | |
"Bonjour #{name}, tu as l'air confus" | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
musicians = ["David Gilmour", "Roger Waters", "Richard Wright", "Nick Mason"] | |
# EACH OPERATOR | |
musicians.each do |musician| | |
puts "Hello #{musician}" | |
end | |
musicians.each { |musician| puts "Hello #{musician}" } | |
# EACH_WITH_INDEX | |
musicians.each_with_index do |musician, index| | |
puts "#{index + 1} - #{musician.downcase}" | |
end | |
# MAP | |
upcase_musicians = musicians.map.object_id do |musician| | |
musician.upcase | |
end | |
# COUNT | |
count_musician = musicians.count do |musician| | |
musician | |
end | |
# SELECT | |
select_musicians = musicians.select do |musician| | |
musician.start_with?("R") | |
end | |
# MANY MORE | |
# http://ruby-doc.org/core-2.5.0/Array.html | |
# http://ruby-doc.org/core-2.5.0/Enumerable.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
musicians = ["Jimmy Page", "Robert Plant", "John Paul Jones", "John Bonham"] # create array of musicians | |
musicians.size == musicians.length # return number of element in array | |
musicians << "ben" # add element to array | |
musicians[0] == musicians.first # return first element | |
musicians.last == musicians[-1] # return last element | |
musicians[1] = "new value" # re-assign the element at index 1 | |
musicians.delete("new value") # /!\ WILL DELETE ALL ELEMENT THAT MATCH /!\ | |
musicians.delete_at(1) # remove element at index 1 | |
musicians.shift # remove first element | |
musicians.pop # remove last element | |
# LOOP OVER ELEMENTS | |
for musician in musicians | |
puts musician | |
end | |
# LOOP OVER INDEXES | |
for index in 0...musicians.length | |
musician = musicians[index] | |
puts "#{index + 1} - #{musician}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment