The real use of binding objects in ruby

Last night, I thought about what, in my opinion, is the advanced features of the ruby ​​language, namely Continuations (callcc) and Binding objects. I mean advanced, because I have a statically typed background of UNs (C #, Java, C ++), I discovered a ruby ​​recently, so these language functions are not very familiar to me.

I am wondering what it might be in reality to use these langages functions. In my experience, everything could have been done using statically typed reservations, but sometimes I don't agree very cleverly. I think I understood the beauty / interest in continuing to read this good article from Sam Ruby: http://www.intertwingly.net/blog/2005/04/13/Continuations-for-Curmudgeons

However, I lost the Binding object. Can someone provide me with some real-world examples of something that can be done using a Binding object, but not very vaguely with langages that lacks the Ruby Binding concept?

I was thinking about rolling back some objects in their original state when something went wrong during the long start-up process, but I'm not sure that this can be implemented using the Binding object, and I think it could be implemented quite cleverly by cloning objects before processing and replacing the modified object with their clones when something goes wrong during processing. So this is not an example that I think.

Thanks in advance for your help.

+5
source share
3 answers

.

class Array
  def debug binding
    each do |arg|
      puts "arg = #{eval(arg, binding).inspect}"
    end
  end
end

Ruby , :

# .. some hairy code you want to debug ...
['user','current_resource', 'user.owns?(current_resource)'].debug(binding)

user = #<User id:1, username: 'joe', ...
current_resource = #<Comment id:20, ...
user.owns?(current_resource) = false

, .

, debug, eval, debug. , , . , , , ...

+5
+8

All Articles