What if you could call any method just by its name, without writing endless if-else statements?
Why Send for calling methods dynamically in Ruby? - Purpose & Use Cases
Imagine you have a program that needs to call different methods based on user input or some changing conditions. You try to write separate code for each method call manually.
This manual way means writing many if-else or case statements. It gets long, hard to read, and easy to make mistakes. Adding new methods means changing lots of code.
Using send lets you call any method by its name stored in a variable. This means one simple line can replace many if-else checks, making your code cleaner and easier to update.
if action == 'start' start elsif action == 'stop' stop end
send(action)
You can write flexible programs that decide what to do at runtime without messy conditionals.
A game where player commands like 'jump', 'run', or 'attack' are typed, and the program calls the matching method dynamically.
Manual method calls need many condition checks.
send calls methods by name stored in variables.
This makes code shorter, cleaner, and easier to change.