Skip to content

Instantly share code, notes, and snippets.

@Cosmicoppai
Created December 26, 2023 13:09
Show Gist options
  • Save Cosmicoppai/ada0d4abe6d84a65987b606b14e9479b to your computer and use it in GitHub Desktop.
Save Cosmicoppai/ada0d4abe6d84a65987b606b14e9479b to your computer and use it in GitHub Desktop.
proc in ruby
# In Ruby, a Proc is an object that represents a block of code that can be stored in a variable.
# It's a way to create an anonymous function or code block that can be assigned to a variable and passed around in your program.
# Procs are instances of the Proc class.
# Here's a basic example of using a Proc:
full_name = Proc.new{ |first, last| first + " " + last}
p full_name["Mashiro", "Moritaka"] # we can call proc using [], using the bracket followed by the arguments
p full_name.call("Azuki", "Miho") # we can also call proc using call method
add = Proc.new do |x, y|
x + y
end
def do_operation (a, b, operation)
operation.call(a,b)
end
p do_operation(1,2,add)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment