0Pricing
Ruby Academy · Lesson

Using send and eval

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

Using send and eval is a free Ruby Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

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

2

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)

3

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')

4

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)

5

What is the eval Method?

eval executes a string as Ruby code at runtime.

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

6

Executing Dynamic Code with eval

eval can dynamically define variables and execute expressions.

eval("x = 100")
puts x

7

Defining Methods with eval

Use eval to define methods dynamically.

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

8

9

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.

10

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

Frequently asked questions

Is the “Using send and eval” lesson free?

Yes — the full text of “Using send and eval” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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