Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created October 18, 2018 10:14
Show Gist options
  • Save rodloboz/56b424056d7bd50249450ca3a908fef1 to your computer and use it in GitHub Desktop.
Save rodloboz/56b424056d7bd50249450ca3a908fef1 to your computer and use it in GitHub Desktop.
# YIELD
def bigcount
(0..100000000).sort
end
def timer(callback)
start_time = Time.now
callback
end_time = Time.now
end_time - start_time
end
def timer
start_time = Time.now
yield if block_given?
end_time = Time.now
end_time - start_time
end
def greet(name)
puts "#{yield(name)}"
end
greet("rui") { |name| "Hello, #{name.capitalize}" }
greet("rui") { |name| "Bom dia, #{name.capitalize}" }
countries = ["portugal", "england", "poland", "france"]
# Enumerators
countries.each do |country|
puts country.capitalize
end
countries.each_with_index do |country, index|
puts "#{index + 1} - #{country.capitalize}"
end
upcased_countries = []
countries.each do |country|
upcased_countries << country.upcase
end
# map => returns new array with same length
upcased_countries = countries.map do |country|
country.upcase
end
p upcased_countries
# select =>
countries_with_p = countries.select do |country|
country.upcase.start_with?("P")
end
p countries_with_p
# find => returns an element
country_with_e = countries.find do |country|
country.downcase.start_with?("p")
end
p country_with_e
# do end
p countries.find { |country| country.downcase.start_with?("p") }
# count => returns an integer
p countries.count { |country| country.start_with?("p") }
range = (0..100).to_a
# p range
range_2 = (0...100).to_a
# p range_2
# Arrays
#. 0 1 2
countries = ["portugal", "england", "poland", "france"]
# Reading
# puts countries[1]
for index in (0...countries.length)
puts countries[index]
end
for country in countries
puts country.capitalize
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment