In ruby how do I write a 'do' method in ruby?
ruby, ruby-on-rails, yield
Solution
Pass it a block as an argument
def my_method(&block)
do_something_the_same
yield # calls whatever is inbetween "do" and "end"
end
Problem
I keep writing the same pattern of code in Ruby, which seems like it would benefit from a 'do' style bit of code but I'm not sure how to write the method. I keep doing this pattern of code, which starts and ends with the same lines of code... ``` x.increment!(:step_count) # same each time # ...then some different code each x.update_column(:step_description, "blerg message") # same each time ``` I feel it would benefit from a 'do' something that would look like this... ``` update_steps "blerg message" do # ...then some different code each end ``` And then inside the 'do' each time it does the common code. How would I go about making a method where I can use a 'do'. Thanks! Edit: I think it's important to not close this because I didn't know to search for 'block' or 'yield'. People who may no know these terms may end up searching for 'do' instead.