Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Foundations Ruby Standard Library ERB

Antoine Boillot
Antoine Boillot
10,466 Points

What is in simple words the purpose and functioning of the binding method in Ruby ?

Hi all,

Despite Jason's quick explanation in the Ruby Standard Library module, section ERB, I'm not sure I really understand the purpose and functioning of the binding method in Ruby.

I had a look at the Ruby documentation on the matter, yet it remains a bit blurry.

Could someone enlighten me ?

Thanks a lot,

Cheers,

ps : here is the code I discovered it in

require 'erb'

class BankAccount
    TEMPLATE = <<-TEMPLATE
Bank Account : <%= @name %>
---
<% @transactions.each do |transaction| %>
    Transaction: <%= transaction %>
<% end %>
---
    TEMPLATE

    def initialize(name)
        @name = name
        @transactions = []
    end

    def deposit(amount)
        @transactions.push(amount)
    end

    def withdraw(amount)
        @transactions.push(-amount)
    end

    def get_binding
        binding
    end

    def display
        ERB.new(TEMPLATE).result(get_binding)
    end
end

3 Answers

Antoine, I had the same question. The video didn't do much to really explain what was going on with that method. This question and answer on stackoverflow helped me to make sense of it: http://stackoverflow.com/questions/1338960/ruby-templates-how-to-pass-variables-into-inlined-erb

In that example, you see an each block that is passing in a variable 'current'. And then the programmer is trying to access that current variable in his erb template. Adding a binding method and passing it into the erb result allows him access to the 'current' variable. Without it, he gets an error, because there is no "communication" between the block of code running the erb.result (in the each do iterator) and the ruby code in the template defined within the class.

It has to do with the variable scope. I hope that makes some sense to you! Maybe it will at least point you in the right direction to figure it out.

Antoine Boillot
Antoine Boillot
10,466 Points

Makes perfect sense now. Thanks a lot ! :)

Great answer very useful for understand binding concept...

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

I'm not really sure why you would use it in pure Ruby (like in the video), but this is something that prepares you for Rails - in Rails, you will often use ERB tags inside HTML templates to evaluate Ruby code and get dynamic content. You would use the tags with = sign when you want to print out the value that you get, and tags without the = sign to just do things like loops, which should not print out stuff in your HTML :).

This works kind of the same way as

puts "#{something}"

If something is a Ruby variable, the above code will simply print out the value of that variable.

Antoine Boillot
Antoine Boillot
10,466 Points

Thanks a lot Maciej Czuchnowski ! I got your point, however, I do not understand the link with the binding (I'm a newbie :) !) Am I missing something here ?

cc Jason Seifer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Sorry, never got that deep into ERB :)