Created
May 8, 2021 03:31
-
-
Save creaturenex/9689b9ea3a279c7301c0814087d3dbd0 to your computer and use it in GitHub Desktop.
issue I was having
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
You have an application that uses a #fetch method to retrieve a collection of data from some external source and a #save method that saves any modified data when you finish. To make updates easier, you have several methods that update the data based on different search criteria. So far, you have something like this: | |
Copy Code | |
def update_data(select_string, block) | |
data = fetch(select_string) | |
data.each # you can update or replace this line | |
save(data) | |
end | |
def update_by_type(type) # you can update this method | |
update_data("employee_type = #{type}") | |
end | |
def update_by_location(location) # you can update this method | |
update_data("employee_location = #{location}") | |
end | |
# Example calls - do not modify these lines | |
update_by_type("Manager") do |employee| | |
employee.salary *= 1.25 | |
end | |
update_by_location("Oregon") do |employee| | |
employee.salary *= 1.10 | |
end | |
You want to use the #each method in #update_data rather than write an explicit loop, but this means that you somehow need to pass a block through two different methods before you can invoke it. A simple yield won't work in this case. | |
Finish implementing the #update_data, #update_by_type, and #update_by_location methods. | |
def update_data(select_string, &block) | |
data = fetch(select_string) | |
data.each do |item| | |
block.call(item) # per Ruby documentation | |
end | |
save(data) | |
end | |
def update_by_type(type, &block) # you can update this method | |
update_data("employee_type = #{type}", &block) | |
end | |
def update_by_location(location, &block ) # you can update this method | |
update_data("employee_location = #{location}", &block) | |
end | |
This gave me issue because I know you can pass arguments to a block with yield(arg) within a method. I do not recall seeing using block.call(arg) with an argument pass in all my practice problems or challenges only block.call itself . Looking at the documentation shows it is possible Proc.call. The each threw me off because I knew we had to pass an argument but didn't think yield was a appropriate answer. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment