Tag: ruby

attr_accessor强类型Ruby on Rails

只是想知道是否有人可以通过强类型的视图阐明Ruby on Rails中getter setter的基础知识。 我对rails上的ruby非常新,并且主要对.NET有很好的理解。 例如,我们假设我们有一个名为Person的.net类 class Person { public string Firstname{get;set;} public string Lastname{get;set;} public Address HomeAddress{get;set;} } class Address { public string AddressLine1{get;set;} public string City{get;set;} public string Country{get;set;} } 在Ruby中,我会写这个 class Person attr_accessor :FirstName attr_accessor :LastName attr_accessor :HomeAddress end class Address attr_accessor :AddressLine1 attr_accessor :City attr_accessor :Country end 查看Person类的Ruby版本如何指定访问器方法FirstName,LastName和HomeAddress的类型? 如果我要使用这个类,我可以将任何类型提供给HomeAddress,但我希望这个访问器方法只接受TYPE地址。 有什么建议 ? […]

在Ruby中以块的forms读取文件

我需要读取MB块中的文件,有没有更简洁的方法在Ruby中执行此操作: FILENAME=”d:\\tmp\\file.bin” MEGABYTE = 1024*1024 size = File.size(FILENAME) open(FILENAME, “rb”) do |io| read = 0 while read < size left = (size – read) cur = left < MEGABYTE ? left : MEGABYTE data = io.read(cur) read += data.size puts "READ #{cur} bytes" #yield data end end

如何在rails中添加条件where子句

我是一个铁杆新手,我正试图在带有rails的桌面上进行搜索,而我只是使用我的sql知识来做到这一点。 但这似乎不像铁路或ruby甚至…… 有没有更好的方法来做我正在做的事情? (基本上,只有在填充时才将日期参数传递给sql) def search(begin_date=nil, end_date=nil) subject = ” and created_at ” if !(begin_date.nil? || end_date.nil?) where_part = subject + “BETWEEN :begin_date AND :end_date” else if (begin_date.nil? && end_date.nil?) where_part = “” else if(begin_date.nil?) where_part = subject + ” = :begin_date” end end end end User.joins(places: {containers: {label: :user}}).where(“users.id= :user_id “+where_part, user_id: self.id, begin_date:begin_date, […]

RailsTutorial – 第8.4.3章 – 在集成测试中添加用户后,测试数据库未清除

我对这个很难过。 到目前为止,教程中的所有内容都进展顺利,但是当我将这段代码添加到我的/spec/requests/users_spec.rb文件中时,事情开始向南: describe “success” do it “should make a new user” do lambda do visit signup_path fill_in “Name”, :with => “Example User” fill_in “Email”, :with => “ryan@example.com” fill_in “Password”, :with => “foobar” fill_in “Confirmation”, :with => “foobar” click_button response.should have_selector(“div.flash.success”, :content => “Welcome”) response.should render_template(‘users/show’) end.should change(User, :count).by(1) end end 如果我清除测试数据库(rake db:test:prepare),则所有测试都通过。 但是,如果我再次运行测试,它们会失败,因为测试数据库不会清除上面添加的代码的记录。 我已经google了很多,我发现的大部分内容都指向config.use_transactional_fixtures设置,或指向代码中的嵌套问题。 我很确定这些都不是我的情况。 […]

windows:rails:安装bson_ext时出错

当试图安装bson_ext我看到错误…安装json gem工作正常,这也需要构建原生扩展 – 我已经尝试了一切看到类似的问题,没有很好的答案 $ gem install bson_ext Temporarily enhancing PATH to include DevKit… Building native extensions. This could take a while… ERROR: Error installing bson_ext: ERROR: Failed to build gem native extension. c:/Ruby193/bin/ruby.exe extconf.rb checking for asprintf()… no checking for ruby/st.h… yes checking for ruby/regex.h… yes checking for ruby/encoding.h… yes creating Makefile make generating […]

在Ruby中,没有办法在当前上下文中动态定义局部变量吗?

我想知道是否有一种方法可以让我在当前上下文中动态定义一个先前未定义的变量。 例如: foo # => NameError: undefined method or local variable … # Some method call which sets foo = 1 in the local context foo # => 1 换句话说,假设foo未定义,我正在寻找任何代码,可以让我在不使用foo变量的情况下定义局部变量foo (例如,如果我有一些其他变量bar其值为:foo ,我不得不依赖在那上面设置foo的值)。 似乎eval(‘foo = 1’)或eval(‘foo = 1’, binding)或者在Ruby 2.1中, binding.local_variable_set(:foo, 1)都等效于: 1.times do foo = 1 end 换句话说,它们在新的本地上下文的上下文中设置foo ,使得该值在该上下文之外是不可访问的。 我正在寻找可能吗? 更新:此问题并非特定于任何特定的局部变量上下文(模块/类,方法,过程,块等)。 我有兴趣明确知道它可以或不可以完成的任何背景。

在Rails 4迁移中设置自定义主键时出现问题

我使用postgresql 9.3,Ruby 2.0,Rails 4.0.0。 在阅读了关于在表上设置主键的SO的大量问题之后,我生成并添加了以下迁移: class CreateShareholders < ActiveRecord::Migration def change create_table :shareholders, { id: false, primary_key: :uid } do |t| t.integer :uid, limit: 8 t.string :name t.integer :shares t.timestamps end end end 我还在我的模型中添加了self.primary_key = “uid” 。 迁移成功运行,但是当我使用pgAdmin III连接到数据库时,我看到uid列未设置为主键。 我错过了什么?

如何合并两个哈希没有新的键

我怎么能合并两个哈希导致没有新的密钥,这意味着合并会合并两个哈希中存在的密钥? 例如,我想要以下内容: h = {:foo => “bar”} j = {:foo => “baz”, :extra => “value”} puts h.merge(j) # {:foo => “baz”} 我正在寻找一种非常干净的方法,因为我当前的实现非常混乱。

在Windows XP中安装Ruby Curb gem

我正在尝试使用ruby 1.8进行文件上传,似乎没有好的开箱即用的解决方案。 他们似乎都需要一些第三方补丁或项目分支来工作。 我会使用机械化,但实际上我需要与xml api接口,而机械化似乎专门用于直接处理网页。 我已经尝试过RestClient但它没有开箱即用的多部分表单的function,你必须使用项目的分支才能使它工作。 类似的情况似乎存在于Net:HTTP我认为遏制将是要走的路,因为我可能想要使用multicurl反正我计划做的其他事情。 但是,在我的Windows机器上,我似乎无法安装路边gem。 gem install curb给出一个错误,说我需要指定curl库所在的位置..我已经尝试将选项传递给extconf但到目前为止没有运气。 这是没有任何选项的gem install curb的输出 C:\ruby\lib\ruby\gems\1.8\gems\curb-0.5.4.0>gem install curb Building native extensions. This could take a while… ERROR: Error installing curb: ERROR: Failed to build gem native extension. C:/ruby/bin/ruby.exe extconf.rb checking for curl-config… no checking for main() in curl.lib… no *** extconf.rb failed *** Could not create […]

使用rbenv和ruby-build构建ruby时会出现未定义的符号:SSLv2_method

我正在尝试在带有ARMv7处理器的Synology DS215j上安装ruby 2.2.4或2.3.0。 我使用optware-ng来安装gcc,make,openssl,openssl-dev和zlib。 我按照自述文件中的说明安装了rbenv(版本1.0.0-19-g29b4da7)和ruby-build插件。 这些是使用optware-ng安装的软件包及其版本 binutils – 2.25.1-1 gcc – 5.3.0-6 gconv-modules – 2.21-3 glibc-opt – 2.21-4 libc-dev – 2.21-1 libgmp – 6.0.0a-1 libmpc – 1.0.2-1 libmpfr – 3.1.3-1 libnsl – 2.21-3 libstdc++ – 6.0.21-6 make – 4.1-1 ncurses – 5.7-4 openssl – 1.0.2f-1 openssl-dev – 1.0.2f-1 readline – 6.1-2 ruby – 2.2.0-1 screen […]