Ruby中绑定对象的实际使用 [英] Real-world use of binding objects in ruby

查看:79
本文介绍了Ruby中绑定对象的实际使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨晚,我在想我所认为的高级红宝石语言功能,即Continuations(callcc)和Binding对象。我的意思是高级,因为我有一个静态类型的oo语言背景(C#,Java,C ++),我是最近才发现ruby的,所以我对这些语言功能不是很熟悉。

Last night, I was thinking about what i think are advanced ruby language features, namely Continuations (callcc) and Binding objects. I mean advanced because I have a statically-typed oo langages background (C#, Java, C++), I discovered ruby very recently, so these language features are not very familiar to me.

我想知道这些语言功能在现实中的用途是什么。以我的经验,一切都可以用静态类型的oo语言完成,但我有时并不很聪明。我想我想出了Continuation的优点/趣味,他读了Sam Ruby的那篇好文章: http://www.intertwingly.net/blog/2005/04/13/Continuations-for-Curmudgeons

I'm wondering what could be real-world use of these langages features. In my experience, everything could be done with statically typed oo langages, but I agree not very smartly sometimes. I think I figured out the beauty/interest of Continuation reading that nice article from Sam Ruby : http://www.intertwingly.net/blog/2005/04/13/Continuations-for-Curmudgeons

还是,我米失去了绑定对象。有人可以为我提供一些现实的示例,这些示例可以通过Binding对象巧妙地完成,但是对于缺少ruby Binding概念的语言却不是很巧妙?

Still, i'm lost with Binding object. Can someone provide me with some real-world examples of something that can be smartly done with Binding object but not very smartly with langages missing the ruby Binding concept?

在长时间运行过程中出现问题时,我曾考虑将某些对象回滚到其初始状态,但是我不确定这可以通过Binding对象和我认为可以通过在处理之前克隆对象并在处理过程中出现问题时将修改的对象替换为其克隆来相当巧妙地实现。所以我认为这不是一个有效的示例。

I was thinking of rollbacking some objects to their initial state when something goes wrong during a long runing process, but I'm not sure this could be implemented with Binding object and I think could be implemented quite smartly by cloning objects before the processing and replacing modified object with their clones when something goes wrong during the processing. So this is not a valid example I think.

在此先感谢您的帮助。

推荐答案

我已经使用绑定类来实现调试技巧。

I've used the binding class to implement a debugging hack.

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

您可以使用它检查Ruby代码片段列表以及每个片段返回的内容:

You can use this to inspect a list of snippets of Ruby code along with what each snippet returns:

# .. 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

我发现它对于快速调试非常有用。

I find it very useful for quick debugging.

我需要使用绑定对象来捕获调用 debug 的作用域,以便可以在 eval 在运行调试时。可能还有其他方法可以实现此目的,但是使用绑定既简单又快速。也许还有更好的例子说明绑定对象对于...有用。

I needed to use a binding object to capture the scope where debug is called so it can be used in the eval when debug is run. There are probably other ways to have implemented this but using the binding was easy and fast. There are also probably far better examples of what binding objects are useful for...

这篇关于Ruby中绑定对象的实际使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆