草庐IT

file_contents_as_string

全部标签

ruby - 是否有 Ruby string#blank?方法?

String#blank?非常有用,但存在于Rails中,而不存在于Ruby中。Ruby中是否有类似的东西来替换:str.nil?||str.empty? 最佳答案 据我所知,普通Ruby中没有这样的东西。您可以像这样创建自己的:classNilClassdefblank?trueendendclassStringdefblank?self.strip.empty?endend这将适用于nil.blank?和a_string.blank?您可以将其扩展(就像rails一样)用于true/false和一般对象:classFalseCl

ruby - 什么是枚举器对象? (使用 String#gsub 创建)

我有一个属性数组如下,attributes=["test,2011","photo","198.1x198.1cm","Photo:ManuPKFullScreen"]当我这样做时,artist=attributes[-1].gsub("Photo:")partist我在终端中得到以下输出#想知道为什么我得到一个枚举器对象作为输出?提前致谢。编辑:请注意,我做的不是attributes[-1].gsub("Photo:",""),而是attributes[-1].gsub("Photo:")所以想知道为什么枚举器对象返回到这里(我期待一条错误消息)以及发生了什么。?ruby-1.9.2

ruby - 更新到 cocoapods 0.32.1 后无法运行 sudo pod install,错误为 "You cannot run CocoaPods as root. (CLAide::Help)"

我在更新到最新版本0.32.1后运行sudopodinstall命令时发现了一个问题。早些时候它工作得很好。当我尝试使用旧的cocoapods安装pod时,它要求我更新到最新的cocoapods版本,即0.32.1。在我通过cocoapodsgem更新后,我无法在我的Xcode项目中执行sudopodinstall。它给了我以下错误。±sudopodinstallruby-1.9.3-p0Password:/Users/username/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/gems/1.9.1/gems/claide-0.5.0/lib/claide/

ruby-on-rails - `merge' :string <%= form_for %> helper 的未定义方法 '####'

我有一个表单,在发布时呈现另一个表单。我想做的是将参数从第一种形式传递到第二种形式的某些隐藏字段中。第二种形式是使用form_for形式助手,而我试图做的是让它接受传递给它的参数。表单如下所示:"btnbtn-largebtn-success"%>当我做类似的事情时这个Action给我错误:NoMethodErrorinFind_numbers#createShowingC:/Sites/dentist/app/views/phones/new.html.erbwhereline#17raised:undefinedmethod`merge'for"1231231234":String

ruby - 无法将 Symbol 转换为 String

我有以下Ruby代码,直接取自GettingStartedwithRails指南defcreate@post=Post.new(post_params)@post.saveredirect_to@postendprivatedefpost_paramsparams.require(:post).permit(:title,:text)end当我运行上面的Create时,出现以下错误。can'tconvertSymbolintostring 最佳答案 您似乎正在尝试使用强参数。你得到这个错误cannotconvertsymbolint

ruby - 在 String 子类上覆盖 =~ 方法会产生不一致

我为String的子类覆盖了=~方法:classMyString重写的方法在某些情况下被正确调用:r=/abc/s=~r#=>"Overriddenmethod."s.send(:=~,r)#=>"Overriddenmethod."s.send(:=~,/abc/)#=>"Overriddenmethod."而在其他情况下它被绕过,而是调用String#=~:s=~/abc/#=>0s=~(/abc/)#=>0我可以在Ruby1.8.7、2.1.0上重现这些结果。有人知道为什么会这样吗?是错误吗? 最佳答案 在String#=~的

ruby - kernel_require.rb :55:in `require' : cannot load such file error

我目前使用的是Ruby1.9.3版(尽管我在使用Ruby2.0.0时遇到了同样的问题)。在Windows764位上。我正在关注“TheCucumberBook”并卡在第7.2章-“使用转换删除重复项”。我的文件夹结构如下:\cash_withdrawal\cash_withdrawal\Gemfile\cash_withdrawal\Gemfile.lock\cash_withdrawal\features\cash_withdrawal\features\cash-withdrawal.feature\cash_withdrawal\features\step_definitions

ruby - Homebrew 软件 - 错误的解释器 : No such file or directory

像个白痴一样,我设法删除了我的系统ruby​​安装。我重新安装了xcode,还安装了RVM这样做:$whichruby返回这个:/Users/alex/.rvm/bin/ruby但是,Homebrew似乎还是坏了:$brew-bash:/usr/local/bin/brew:/usr/bin/ruby:badinterpreter:Nosuchfileordirectory 最佳答案 OSX上的系统ruby​​符号链接(symboliclink)到Ruby.framework。由于您重新安装了Xcode,它应该已安装,但您需要恢复符

ruby-on-rails - "require File.expand_path(' ../../config/environment', __FILE__)"到底做什么?

我一直在尝试更深入地了解Ruby,解构样板式rails应用程序似乎是了解编写跨不同文件和目录的Ruby应用程序的一些细节和优雅的好方法。在我当前的应用程序中,文件之间对“requires”的依赖变得有点问题(我发现我需要做一些事情,比如requires'../../../lib/helper'它变得有点丑陋。我注意到Rails应用程序似乎不受此影响。我确实注意到了这条线:requireFile.expand_path('../../config/environment',__FILE__)当我用谷歌搜索时,我发现很多关于Rails启动例程等的解释,但没有关于该行到底做什么的明确描述。在

ruby - Mailchimp API 未替换 mc :edit content sections (using ruby library)

我在用我提供的内容替换Mailchimp中的mc:edit内容区域时遇到问题。电子邮件已发送给订阅者,但所提供的内容均未添加到电子邮件中。谁能看出我可能哪里出错了?这是我正在使用的脚本:campaign=mailchimp.campaigns.create("regular",{"list_id"=>list_id,"subject"=>"EmailTest","from_email"=>"edward@somewhere.com","from_name"=>"Edward","to_name"=>"Thetoname","template_id"=>35089},{"sections