【发布时间】:2011-12-08 00:42:39
【问题描述】:
我正在将一些项目转移到 Rails,我想复制这段 PHP 代码:
http://www.php.net/manual/en/functions.anonymous.php#106046
到目前为止我有这个:
def html (tag, id = "", hclass = "")
hopen = "<#{tag}"
hclose = "</#{tag}>"
unless id == ""
hopen += " id=\"#{id}\""
end
unless hclass == ""
hopen += " class=\"#{hclass}\""
end
hopen += ">"
return lambda { |data| print hopen,data,hclose}
end
我需要创建可变变量,如下所示: PHP
$layout = array('container','header','pmain','lsidebar','rsidebar','footer');
foreach ($layout as $element)
$$element = html ("div", $element);
这是我的 RUBY 原型
layout = [:body, :header, :sidebar, :footer]
##I know this isn't right, but how do I create the dynamic functions like PHP???
layout.each {|x| instance_variable_set "@#{x}", 0 }
另外,我需要调用函数,没有调用方法有没有办法做到这一点?如果我必须嵌套调用,它会很混乱。
h1 = html(:h1)
mainx = html(:div )
puts mainx.class
puts mainx.call(h1.call("Blog!"))
【问题讨论】: