在Ruby中,有没有办法来完成什么`with`确实在Actionscript中? [英] In Ruby, is there a way to accomplish what `with` does in Actionscript?

查看:319
本文介绍了在Ruby中,有没有办法来完成什么`with`确实在Actionscript中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中,我想选择一个块中的默认对象。

在Actionscript中的一个例子是:

 带(板){
    长度= 66;
    宽度= 19;
    fin_system =密码箱;
}  

这是等价于:

  board.length = 66;
board.width = 19;
board.fin_system =密码箱;  

下面是本声明ActionScript中的文件: 的http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#with

我怎样才能做到这一点在Ruby中?

解决方案

  Hash.new.tap办| H |
  H [:名称] =迈克
  H [:语] =红宝石
结束
#=> {:名称=>中迈克:语言=>红宝石}
 

您可以尝试对象#自来水使用Ruby 1.9。

所以,你的情况:

  board.tap办| B |
  b.length个= 66;
  b.width = 19;
  b.fin_system =密码箱
结束
 

In Ruby, I would like to select a default object for a block.

An example in Actionscript is:

with (board) { 
    length = 66; 
    width = 19; 
    fin_system = 'lockbox'; 
}

Which is equivalent to:

board.length = 66;
board.width = 19;
board.fin_system = 'lockbox';

Here is the documentation for this statement in Actionscript: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#with

How can I accomplish this in Ruby?

解决方案

Hash.new.tap do |h|
  h[:name] = "Mike"
  h[:language] = "Ruby"
end
#=> {:name=>"Mike", :language=>"Ruby"} 

You could try Object#tap with Ruby 1.9.

So in your case:

board.tap do |b|
  b.length = 66;
  b.width = 19;
  b.fin_system = "lockbox"
end

这篇关于在Ruby中,有没有办法来完成什么`with`确实在Actionscript中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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