Send for calling methods dynamically
📖 Scenario: Imagine you have a simple calculator that can perform different operations like addition, subtraction, multiplication, and division. You want to call these operations dynamically based on user input.
🎯 Goal: Build a Ruby program that uses send to call calculator methods dynamically.
📋 What You'll Learn
Create a class
Calculator with methods add, subtract, multiply, and divideCreate an instance of
Calculator called calcCreate a variable
operation that stores the method name as a symbolUse
send on calc with operation and two numbers to get the resultPrint the result
💡 Why This Matters
🌍 Real World
Dynamic method calling is useful when you want to choose actions at runtime, like in calculators, menus, or command handlers.
💼 Career
Understanding <code>send</code> helps in metaprogramming and building flexible Ruby applications that adapt to different inputs or commands.
Progress0 / 4 steps