Ruby Academy · Lesson

Using send and eval

Discover how to execute dynamic method calls and evaluate strings as Ruby code.

Lesson 4 of 410 steps

Using send and eval is a free Ruby Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

Dynamic Code Execution

Using send and eval

Ruby provides powerful methods like send and eval to dynamically execute code.

These methods allow calling methods dynamically and evaluating Ruby expressions at runtime.

Using send and eval — illustration 1

What is the send Method?

send allows calling methods dynamically by name.

It can invoke private and public methods.

class Person
  def greet
    'Hello!'
  end
end

p = Person.new
puts p.send(:greet)

Using send with Arguments

send can pass arguments to methods dynamically.

class Person
  def greet(name)
    "Hello, #{name}!"
  end
end

p = Person.new
puts p.send(:greet, 'Alice')

Calling Private Methods with send

send can bypass method visibility rules and call private methods.

class Person
  private
  def secret
    'This is a secret!'
  end
end

p = Person.new
puts p.send(:secret)

What is the eval Method?

eval executes a string as Ruby code at runtime.

code = "5 + 10"
puts eval(code) # Outputs: 15

Executing Dynamic Code with eval

eval can dynamically define variables and execute expressions.

eval("x = 100")
puts x

Defining Methods with eval

Use eval to define methods dynamically.

eval("def dynamic_method; 'Hello from eval'; end")
puts dynamic_method

Security Risks of send and eval

Using send and eval improperly can introduce security risks:

  • eval can execute untrusted code, leading to security vulnerabilities.
  • send can call private or sensitive methods.
  • Use them cautiously to avoid unintended behavior.

Send and Eval Recap

Congratulations!

You have learned how to use send and eval for dynamic method execution.

This concludes the Metaprogramming in Ruby section.

Using send and eval — illustration 10
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Using send and eval” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Using send and eval”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “Using send and eval”?

Discover how to execute dynamic method calls and evaluate strings as Ruby code. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Using send and eval” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What is Metaprogramming?
  2. Dynamic Method Definition
  3. Open Classes and Monkey Patching
  4. Using send and eval
← Back to Ruby Academy