Tag: ruby

each_with_object应该如何工作?

我想知道如何使用each_with_object 。 我试着用它来做一个非常简单的总和,所以我写道: > (1..3).each_with_object(0) {|i,sum| sum+=i} => 0 嘿,我认为结果将是6! 我的错误在哪里?

使用AWS Elastic Beanstalk和Ruby容器设置私有Github访问

通过最近使用Git设置用于Ruby部署的AWS Elastic Beanstalk的教程 ,我只是从我的CI服务器设置了一个Elastic Beanstalk环境。 但是,应用程序无法启动。 我浏览了日志,发现bundle install失败并显示错误消息。 获取git@github.com:example / private-repository.git主机密钥validation失败。 致命:远程终端意外挂断[31mGit错误:命令git clone ‘git@github.com:example/private-repository.git’ “/var/app/ondeck/vendor/cache/ruby/1.9.1/cache/bundler/git/private-repository-e4bbe6c2b13bb62664e39e345c1b01d80017934c” –bare –no-hardlinks clone’git@github.com git clone ‘git@github.com:example/private-repository.git’ “/var/app/ondeck/vendor/cache/ruby/1.9.1/cache/bundler/git/private-repository-e4bbe6c2b13bb62664e39e345c1b01d80017934c” –bare –no-hardlinks目录/ var / app / ondeck中的git clone ‘git@github.com:example/private-repository.git’ “/var/app/ondeck/vendor/cache/ruby/1.9.1/cache/bundler/git/private-repository-e4bbe6c2b13bb62664e39e345c1b01d80017934c” –bare –no-hardlinks失败。[0m 我的Rails应用程序的Gemfile包含对我在Github上拥有的几个私有存储库上托管的gemified插件的引用。 就像是 gem’partgemname’,: git =>’git@github.com:example / private-repository.git’ 我遇到过与Capistrano部署类似的问题,这些问题通过设置ssh_options[:forward_agent] = true 。 AWS Elastic Beanstalk Ruby容器通过放在.ebextensions下的自定义.config文件支持自定义配置。 在这种情况下,设置SSH转发代理会有所帮助吗? 在启动Elastic Beanstalk环境时,还有其他替代方法可以访问私有Github存储库吗? 更新1:我刚刚检查了启动bundle install的用户。 发现脚本/opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh以root用户/opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh启动bundle […]

我如何找到ruby翻译?

在ruby脚本中,如何获取ruby解释器的路径? 示例脚本: #!/path/to/ruby puts `#{RUBY_INTERPRETER_PATH} -e “puts ‘hi'”` #EOF 其中RUBY_INTERPRETER_PATH是一种神秘的查找/path/to/ruby 。 不过,这只是一个例子。 在这种情况下我意识到我可以将/path/to/ruby复制到脚本中,但我不想这样做。 无论#!是什么,我希望这个“正确”工作#! 线说。 即使在windows下运行。 再见!

无法构建gem原生扩展

最终的结果是我试图在我的OS 10.7.3上安装Ruby on Rails,但是遇到了各种各样的问题。 另外,我从App Store新安装了XCode 4.3。 当我启动终端时,输入: sudo gem install rails 它写出: Building native extensions. This could take a while… ERROR: Error installing rails: ERROR: Failed to build gem native extension. /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb mkmf.rb can’t find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/json-1.6.5 for inspection. Results logged to […]

使用Ruby获取网页内容 – 我遇到了麻烦

我想从这个*页面中删除内容。 我查找的所有内容都提供了解析CSS元素的解决方案; 但是,该页面没有。 这是我发现的唯一看起来应该工作的代码: file = File.open(‘http://hiscore.runescape.com/index_lite.ws?player=zezima’, “r”) contents = file.read puts contents 错误: tracker.rb:1:in ‘initialize’: Invalid argument – http://hiscore.runescape.com/index_lite.ws?player=zezima (Errno::EINVAL) from tracker.rb:1:in ‘open’ from tracker.rb:1 * http://hiscore.runescape.com/index_lite.ws?player=zezima 如果您尝试将其格式化为post中的链接,则由于某种原因它无法识别URL中的下划线(_)。

ruby中的条件链接

有没有一种在Ruby中有条件地链接方法的好方法? 我想要在function上做什么 if a && b && c my_object.some_method_because_of_a.some_method_because_of_b.some_method_because_of_c elsif a && b && !c my_object.some_method_because_of_a.some_method_because_of_b elsif a && !b && c my_object.some_method_because_of_a.some_method_because_of_c etc… 因此,根据一些条件,我想弄清楚在方法链中调用哪些方法。 到目前为止,我以“好的方式”做到这一点的最佳尝试是有条件地构建方法的字符串,并使用eval ,但肯定有更好的,更ruby的方式?

ruby是否自动调用initialize方法?

如果在类定义中包含initialize方法,是否需要显式初始化对象?

在Ruby中重新分配常量时抛出exception?

我早就意识到Ruby中的“常量”(即大写的变量名)并不是真正的常量。 与其他编程语言一样,对象的引用是唯一存储在变量/常量中的东西。 (补充工具栏:Ruby确实具有“冻结”被修改引用对象的function,据我所知,这不是许多其他语言提供的能力。) 所以这是我的问题:当你为一个常量重新赋值时,你得到一个警告: >> FOO = ‘bar’ => “bar” >> FOO = ‘baz’ (irb):2: warning: already initialized constant FOO => “baz” 有没有办法强制Ruby抛出exception而不是打印警告? 很难弄清楚为什么有时会进行重新分配。

使用Ruby将数字转换为单词?

如何将数字转换为ruby中的单词? 我知道某处有一块gem。 试图在没有gem的情况下实现它。 我只需要用英语中的数字来表示整数。 发现这个,但它非常凌乱。 如果您对如何实现更清洁易读的解决方案有任何想法,请分享。 Ruby – convert number to english word 这是我一直在努力的方面。 但是在实施量表时遇到一些问题。 代码仍然是一团糟。 我希望它在正常运行时更具可读性。 class Numberswords def in_words(n) words_hash = {0=>”zero”,1=>”one”,2=>”two”,3=>”three”,4=>”four”,5=>”five”,6=>”six”,7=>”seven”,8=>”eight”,9=>”nine”, 10=>”ten”,11=>”eleven”,12=>”twelve”,13=>”thirteen”,14=>”fourteen”,15=>”fifteen”,16=>”sixteen”, 17=>”seventeen”, 18=>”eighteen”,19=>”nineteen”, 20=>”twenty”,30=>”thirty”,40=>”forty”,50=>”fifty”,60=>”sixty”,70=>”seventy”,80=>”eighty”,90=>”ninety”} scale = [000=>””,1000=>”thousand”,1000000=>” million”,1000000000=>” billion”,1000000000000=>” trillion”, 1000000000000000=>” quadrillion”] if words_hash.has_key?(n) words_hash[n] #still working on this middle part. Anything above 999 will not work elsif n>= 1000 print n.to_s.scan(/.{1,3}/) […]

Ruby:两个范围之间的交集

在ruby中,给定两个日期范围,我想要表示两个日期范围的交集的范围,或者如果没有交叉则为nil。 例如: (Date.new(2011,1,1)..Date.new(2011,1,15)) & (Date.new(2011,1,10)..Date.new(2011,2,15)) => Mon, 10 Jan 2011..Sat, 15 Jan 2011 编辑:应该说我希望它也适用于DateTime,所以间隔可以缩短到分钟和秒: (DateTime.new(2011,1,1,22,45)..Date.new(2011,2,15)) & (Date.new(2011,1,1)..Date.new(2011,2,15)) => Sat, 01 Jan 2011 22:45:00 +0000..Tue, 15 Feb 2011