Skip to content

Instantly share code, notes, and snippets.

@QuillyT
Last active March 19, 2021 17:36
Show Gist options
  • Save QuillyT/a967d2836b7b100483c4ca79d4d8b508 to your computer and use it in GitHub Desktop.
Save QuillyT/a967d2836b7b100483c4ca79d4d8b508 to your computer and use it in GitHub Desktop.
Blocks in Ruby
def get_items_from_api(page=0, page_size=10)
start = page * page_size
entire_list = (1..30).to_a
slice_of_list = entire_list.slice(start, page_size)
slice_of_list
end
def run_0
page = 0
loop do
paginated_list = get_items_from_api(page)
break if paginated_list.empty?
puts "Working with this array: #{paginated_list}"
paginated_list.each do |item|
puts "Working with this item: #{item}"
end
page +=1
end
end
# this method accepts a block that loops through an enumerable
def looper(&heaven)
return unless block_given?
page = 0
page_size = 10
loop do
paginated_list = get_items_from_api(page, page_size)
break if paginated_list.empty?
heaven.call(paginated_list) # yield(paginated_list)
page += 1
end
end
def run_0
page = 0
loop do
paginated_list = get_items_from_api(page)
break if paginated_list.empty?
puts "Working with this array: #{paginated_list}"
paginated_list.each do |item|
puts "Working with this item: #{item}"
end
page +=1
end
end
def run_1
looper do |items|
puts "working with this set of items: #{items}"
end
end
def run_2
looper{|items| puts "another way to work with: #{items}"
end
############ FILES
begin
file = File.open(file_name, "w")
file.write("your text")
rescue IOError => e
#some error occur, dir not writable etc.
ensure
file.close unless file.nil?
end
File.open(file_name, 'w') do |f|
f.write "writing line to file!"
rescue IOError
#handle error
end
def my_method(f)
f.write("write this!")
rescue IOError
# handle some how
end
File.open(file_name, 'w', &my_method)
######################## Transactions
def transfer_money
ActiveRecord::Base.transaction do
john.update!(money: john.money + 100)
ted.update!(money: ted.money - 100)
rescue SpecialError => e
# handle in a way
rescue ActiveRecord::NotFound
# notify devs
end
end
######################### Tap
def make_a_user
u = User.new
u.build_profile
u.store_avatar
u.send_email_confirmation
u.any_other_setup_required_when_creating_a_new_user
return u
end
def make_a_user_2
User.new.tap do |u|
u.build_profile
u.store_avatar
u.send_email_confirmation
u.any_other_setup_required_when_creating_a_new_user
end
end
u = User.new(args).tap(:&save)
u = User.new(args)
u.save
return u
User.where(name: "Steve").yield_self do |scope|
if quentin_is_awesome?
scope.sort(:awesome)
else
scope.sort(:id)
end
end.where(state: 'RI')
a = [
User.where(name: "Steve"),
if quentin_is_awesome?
User.sort(:awesome)
else
User.sort(:id)
end,
User.where(state: "RI")
].compact.reduce(:&merge)
#Debugging chaining methods
query = User.where(status: "active").where(first_name: "John").where(state: "MD")
query = User.where(status: "active")
puts "status - #{query.count}"
query = query.where(first_name: "Jonathan")
puts "first_name - #{query.count}"
query = query.where(state: "MD")
puts "state - #{query.count}"
u.where(status: "active").tap{|q| puts "active count: #{q.count}"}.
where(state: "first").tap{|q| puts "state count: #{q.count}"}
where(first_name: "John" ).tap{|q| puts "first_name count: #{q.count}"}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment