如何在rails中使用变量作为对象属性?

我有一个带有属性’home_address_country’的PaymentDetail模型,所以我可以使用 @payment_detail.home_address_country //where @payment_detail is object of that model. 我想用这样的东西:— country_attribute=address_type+”_address_country” //where address type is equal to ‘home’ @payment_detail.”#{country_attribute}” 均值属性名称存储在变量中。 我怎样才能做到这一点? 编辑 country_attribute=address_type+”_address_country” country_list=Carmen::country_names eval(“@#{country_attribute} = #{country_list}”)

设计回电

当用户登录和退出时,设计是否有回叫? 这就是我提出的: Warden::Manager.after_authentication do |user,auth,opts| user.update_attribute(:currently_signed_in, true) end Warden::Manager.before_logout do |user,auth,opts| user.update_attribute(:currently_signed_in, false) end 这就是我跟踪当前登录用户的方法。

Rails 3.1和Ruby 1.9.3p125:ruby-debug19仍然崩溃,“未找到符号:_ruby_threadptr_data_type”

可能重复: 使用Ruby 1.9.3进行ruby-debug? 我听说有关ruby 1.9.3p125有解决ruby-debug19问题的传言,所以根据RVM网站上的说明,我重新安装了1.9.3: $ rvm reinstall 1.9.3 –patch debug –force-autoconf $ ruby -v ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.2.0] 然后: gem install ruby-debug19 将此条目添加到我的Gemfile: gem ‘ruby-debug19’ 然后: $ rails server -u => Booting WEBrick => Rails 3.1.3 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown […]

安装RVM(Ruby版本管理器)

有人可以将此翻译成我需要采取的可管理步骤: ~ Wayne You must now finish the install manually: 1) Place the folowing line at the end of your shell’s loading files(.bashrc or .bash_profile for bash and .zshrc for zsh), after all path/variable settings: [[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm Please note that this must only occur once – so, you only need […]

使用WWW:Mechanize将文件下载到磁盘而不先将其全部加载到内存中

我正在使用Mechanize来方便下载某些文件。 目前我的脚本使用以下行实际下载文件… agent.get(‘http://example.com/foo’).save_as ‘a_file_name’ 但是,在将完整文件转储到磁盘之前,会将其下载到内存中。 你如何绕过这种行为,直接下载到磁盘? 如果我需要使用WWW以外的东西:Mechanize那么我将如何使用WWW:Mechanize的cookies?

无法安装pg gem,“mkmf.rb无法找到ruby的头文件”(Mac OSX 10.6.5)

我刚刚升级到Rails 3,并对MacPorts,gem和数据库进行了一些混乱。 我扔掉了所有的gem并将它们安装得很新鲜。 除了pg gem的要求外,一切似乎都没问题。 在为PostgreSQL创建一个新的Rails 3项目后,服务器无法启动,抱怨丢失的pg gem。 做bundle install ,它会拖延一段时间,当然,在pg gem上失败了。 Installing pg (0.10.0) with native extensions /Library/Ruby/Site/1.8/rubygems/installer.rb:483:in `build_extensions’: ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError) /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/pg-0.10.0 for inspection. Results logged to /Library/Ruby/Gems/1.8/gems/pg-0.10.0/ext/gem_make.out from /Library/Ruby/Site/1.8/rubygems/installer.rb:446:in `each’ […]

在嵌套对象中使用自定义to_json方法

我有一个使用Ruby标准库中的Set类的数据结构。 我希望能够将我的数据结构序列化为JSON字符串。 默认情况下,将序列化设置为数组: >> s = Set.new [1,2,3] >> s.to_json => “[1,2,3]” 在尝试反序列化之前哪个是好的。 所以我定义了一个自定义的to_json方法: class Set def to_json(*a) { “json_class” => self.class.name, “data” => { “elements” => self.to_a } }.to_json(*a) end def self.json_create(o) new o[“data”][“elements”] end end 哪个效果很好: >> s = Set.new [1,2,3] >> s.to_json => “{\”data\”:{\”elements\”:[1,2,3]},\”json_class\”:\”Set\”}” 直到我把Set放入哈希或其他东西: >> a = { ‘set’ => s […]

拆分Ruby字符串时如何保留分隔符?

我有这样的文字: content = “Do you like to code? How I love to code! I’m always coding.” 我试图把它拆分成一个? 或. 或者! : content.split(/[?.!]/) 当我打印出结果时,标点符号分隔符丢失了。 你喜欢编码吗? 我多喜欢编码 我总是在编码 我怎样才能保留标点符号?

使用`Array.new(n,Array.new)`创建矩阵

我通过执行以下操作创建了一个数组: @gameboard = Array.new(3, Array.new(3, ” “)) 我尝试分配这样的值,我得到了这个: @gameboard[0][2] = “X” @gameboard #=> [[” “, ” “, “X”], [” “, ” “, “X”], [” “, ” “, “X”]] 当我以不同方式声明数组时, @gameboard = [[” “, ” “, ” “], [” “, ” “, ” “], [” “, ” “, ” “]] 我得到这个结果: @gameboard[0][2] = “X” @gameboard # […]

如何将Ruby中的数组排序为特定的顺序?

我想按照另一个数组中给出的特定顺序对数组进行排序。 EX:考虑一个数组 a=[“one”, “two”, “three”] b=[“two”, “one”, “three”] 现在我想按’b’的顺序对数组’a’进行排序,即 a.each do |t| # It should be in the order of ‘b’ puts t end 所以输出应该是 two one three 有什么建议?