我有一个内联到Ruby代码的ERB模板:
require 'erb'
DATA = {
:a => "HELLO",
:b => "WORLD",
}
template = ERB.new <<-EOF
current key is: <%= current %>
current value is: <%= DATA[current] %>
EOF
DATA.keys.each do |current|
result = template.result
outputFile = File.new(current.to_s,File::CREAT|File::TRUNC|File::RDWR)
outputFile.write(result)
outputFile.close
end
Run Code Online (Sandbox Code Playgroud)
我无法将变量"current"传递给模板.
错误是:
(erb):1: undefined local variable or method `current' for main:Object (NameError)
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
有没有快速的方法来获取Ruby中的数组中的所有其他条目?奇数或偶数条目值包含在奇数中.我希望能够像这样使用它:
array1 += array2.odd_values
Run Code Online (Sandbox Code Playgroud)
要么
puts array2.odd_values.join("-")
Run Code Online (Sandbox Code Playgroud)
例如
更新
这完全是我所追求的,但我确信有一个较短的版本.
array1.each_with_index do |item,index|
if (index %2 ==0) then
array2.push(item)
end
end
Run Code Online (Sandbox Code Playgroud) 所有以下API都做同样的事情:打开一个文件并为每一行调用一个块.有什么偏好我们应该使用一个而不是另一个?
File.open("file").each_line {|line| puts line}
open("file").each_line {|line| puts line}
IO.foreach("file") {|line | puts line}
Run Code Online (Sandbox Code Playgroud) 我跑到gem update --system更新到Rubygems 1.5.0,每次运行任何bundle命令后我得到:
rvm/gems/ruby-1.8.7-p249/gems/bundler-1.0.9/lib/bundler/ui.rb:56: uninitialized constant Gem::SilentUI (NameError)
其他人遇到过这个问题吗?
有没有更好的方法来写这个:
if myarray.include? 'val1' ||
myarray.include? 'val2' ||
myarray.include? 'val3' ||
myarray.include? 'val4'
Run Code Online (Sandbox Code Playgroud) 我有一个Ruby哈希,看起来像:
{ "id" => "123", "name" => "test" }
Run Code Online (Sandbox Code Playgroud)
我想将其转换为:
{ :id => "123", :name => "test" }
Run Code Online (Sandbox Code Playgroud) 当我管理工头时,我得到以下内容:
> foreman start
16:47:56 web.1 | started with pid 27122
Run Code Online (Sandbox Code Playgroud)
只有当我停止它(通过ctrl-c)它才会显示我缺少的东西:
^CSIGINT received
16:49:26 system | sending SIGTERM to all processes
16:49:26 web.1 | => Booting Thin
16:49:26 web.1 | => Rails 3.0.0 application starting in development on http://0.0.0.0:5000
16:49:26 web.1 | => Call with -d to detach
16:49:26 web.1 | => Ctrl-C to shutdown server
16:49:26 web.1 | >> Thin web server (v1.3.1 codename Triple Espresso)
16:49:26 web.1 | >> Maximum connections set to 1024
16:49:26 web.1 | …Run Code Online (Sandbox Code Playgroud) 我正在评估Slim作为个人项目中HAML的替代品,它似乎不像HAML那样优雅地处理HTML5数据属性.我希望有人也可能碰到这个,或者可能已经知道我在他们的文档中还没有找到的选项/语法.
HAML允许您通过使用嵌套哈希来定义HTML 5数据属性,如下所示:
%a{data: {key1: 'val', key2: 'val'}}
导致
<a data-key1='val' data-key2='val'></a>
我有一个变量id,我想将它用作散列中的键,以便分配给变量的值用作散列的键.
例如,如果我有变量,id = 1那么所需的结果将是{ 1: 'foo' }.
我试过创建哈希,
{
id: 'foo'
}
Run Code Online (Sandbox Code Playgroud)
但是,这并不工作,而不是导致与符号的哈希:id来'foo'.
我可以发誓我以前做过这件事,但我完全是在画一个空白.
我最近升级到10.10优胜美地测试版,但是我在安装Nokogiri时遇到了麻烦.我正在使用RVM和Ruby 1.9.3.我也按照这里的步骤尝试按照Nokogiri主页上的说明进行操作.
我通过自制程序安装了libxml2(2.9.1)和libxslt(1.1.28),并尝试使用我的Xcode 5安装和Xcode 6 beta中的命令行工具.
gem install nokogiri -v '1.5.5'
Building native extensions. This could take a while...
ERROR: Error installing nokogiri:
ERROR: Failed to build gem native extension.
/Users/grantdavis/.rvm/rubies/ruby-1.9.3-p362/bin/ruby extconf.rb
checking for libxml/parser.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
--with-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir …Run Code Online (Sandbox Code Playgroud)