Rails模型中self的价值是什么?为什么没有明显的实例方法可用?

我的rails 3.1.6应用程序中有一个自定义访问器方法,它为属性赋值,即使该值不存在.my_attr属性是一个序列化的哈希值,除非指定了空值,否则应与给定值合并,在这种情况下,它会将当前值设置为空值。 (增加了检查以确保值是它们应该是什么,但为了简洁而删除,因为它们不是我的问题的一部分。)我的setter定义为: def my_attr=(new_val) cur_val = read_attribute(:my_attr) #store current value #make sure we are working with a hash, and reset value if a blank value is given write_attribute(:my_attr, {}) if (new_val.nil? || new_val.blank? || cur_val.blank?) #merge value with new if cur_val.blank? write_attribute(:my_attr, new_val) else write_attribute(:my_attr,cur_val.deep_merge(new_val)) end read_attribute(:my_attr) end 这段代码按原样运行,但是当我使用self.write_attribute()时则不行。 然后我收到以下错误: NoMethodError: private method `write_attribute’ […]

字符串count()方法

忙着学习Ruby ……文档有一个例子: “你好世界”.count(“lo”,“o”)返回2返回2怎么回事? 在我的例子中,我将:“Lennie”.count(“Le”,“ie”)返回2。 伯爵在这方面如何运作?

为什么’bundle install –without production’命令在更新后不需要’–without production’?

为什么不是第二个命令 $ bundle install 不需要 –without production (因为这是它在教程中的用法,所以我认为它是正确的)不需要它背后发生了什么/推理? 来自http://ruby.railstutorial.org/chapters/a-demo-app#sec-demo_users_resource $ bundle install –without production $ bundle update $ bundle install

rbenv:ruby:命令未找到

我在本地目录上使用rbenv和ruby版本1.9.1-p378。 命令ruby -v给出以下错误: rbenv: ruby: command not found The `ruby’ command exists in these Ruby versions: 2.0.0-p353 任何线索为什么会这样? bundle,rails命令也不起作用。 命令rbenv版本 : * 1.9.1-p378 (set by /home/user/Desktop/r1/noko1/.ruby-version) 2.0.0-p353

如何在启动rails app时解决内存膨胀问题?

我在运行ruby 2.3.3和Rails 4.1.13的Heroku上使用Rails应用程序时出现内存问题。 当我在Heroku上加载应用程序时,它的启动速度约为430 Mb,但在那里相当稳定 – 但仍然在Heroku上达到512 Mb(正如预期的那样)。 我的主要问题是:如何在我的应用程序的BOOT期间精确定位并减少内存使用量? 即我如何减少启动时使用的内存量? 我已经研究了大多数gem,例如出轨,oink,内存分析器和stackprof但是最有帮助的是Heroku上的Scout附加组件,用于查找内存膨胀和内存饥饿控制器等。通过这个我已经设法减少应用程序在内存方面增长的数量,但减少启动时使用的内存量。 我知道gem是在启动时加载的,gem越多,越大,启动时使用的内存就越多。 运行bundle exec derailed bundle:mem给我这个: TOP: 92.3008 MiB carrierwave-aws: 16.7461 MiB carrierwave/storage/aws: 16.6094 MiB aws-sdk-core/s3: 7.9414 MiB aws-sdk-resources: 2.5703 MiB aws-sdk-core: 2.5117 MiB jmespath: 1.8867 MiB jmespath/nodes: 1.1406 MiB jmespath/nodes/function: 0.3867 MiB seahorse/client/base: 0.9414 MiB seahorse/client/plugins/net_http: 0.5273 MiB aws-sdk-core/xml/parser/engines/rexml: 0.8633 MiB rexml/document: 0.8516 MiB […]

我可以在蛋白石中使用Rubygem吗?

有opal-irb和opal-jquery和vienna但有没有办法通过Opal直接在浏览器中使用gems?

在Ruby中使用冒号排序方法

我正在尝试将Bubble排序方法实现为Ruby的简单编码问题,但我遇到了一些麻烦。 我理解的想法是查看第一个元素的值并将其与第二个元素的值进行比较,然后相应地交换它们,但我似乎无法在实际问题中这样做。 有人愿意提供一个关于如何在Ruby中工作的简短示例吗?

在Rails中与同一个类进行多个关联的最佳实践?

我认为我的问题最好被描述为一个例子。 假设我有一个名为“Thing”的简单模型,它有一些简单数据类型的属性。 就像是… Thing – foo:string – goo:string – bar:int 这并不难。 db表将包含三列具有这三个属性的列,我可以使用@ thing.foo或@ thing.bar等访问它们。 但是我试图解决的问题是当“foo”或“goo”不再包含在简单数据类型中时会发生什么? 假设foo和goo代表相同类型的对象。 也就是说,它们都只是具有不同数据的“Whazit”实例。 所以现在看起来像这样…… Thing – bar:int 但现在有一个名为“Whazit”的新模型看起来像这样…… Whazit – content:string – value:int – thing_id:int 到目前为止,这一切都很好。 现在这里是我被困住的地方。 如果我有@thing,我怎么设置它来引用我的2个Whazit实例的名称(为了记录,“业务规则”是任何Thing总是有2个Whazits)? 也就是说,我需要知道我的Whazit是否基本上是foo或goo。 显然,我不能在当前的设置中做@ thing.foo,但我认为这是理想的。 我最初的想法是给Whazit添加一个“名字”属性,这样我就可以得到与我的@thing相关的Whatzits,然后通过这个名字选择我想要的Whazit。 这看起来很难看。 有没有更好的办法?

Ruby Programming Techniques:简单但不那么简单的对象操作

我想创建一个对象,让我们说一个Pie。 class Pie def initialize(name, flavor) @name = name @flavor = flavor end end 但馅饼可分为8块,半块或整块馅饼。 为了争论,我想知道如何为每个Pie对象提供每1/8,1 / 4或每个整体的价格。 我可以这样做: class Pie def initialize(name, flavor, price_all, price_half, price_piece) @name = name @flavor = flavor @price_all = price_all @price_half = price_half @price_piece = price_piece end end 但是现在,如果我要创建十五个Pie对象,我会通过使用诸如此类的方法随机取出某些部分 getPieceOfPie(pie_name) 我怎样才能生成所有可用馅饼的价值? 最终使用如下方法: myCurrentInventoryHas(pie_name) # output: 2 whole strawberry pies […]

库没有加载错误 – 错误的ruby

我正在使用RVM和Ruby 1.9.3 p448,但是当我尝试运行我的rails服务器时,我收到此错误(加上其他一些东西) 9): Library not loaded: /Users/sashafklein/.rvm/rubies/ruby-1.9.3-p327/lib/libruby.1.9.1.dylib (LoadError) Referenced from: /Users/sashafklein/rails//vendor/bundle/ruby/1.9.1/gems/posix-spawn-0.3.6/lib/posix_spawn_ext.bundle Reason: image not found – /Users/sashafklein/rails//vendor/bundle/ruby/1.9.1/gems/posix-spawn-0.3.6/lib/posix_spawn_ext.bundle 奇怪的是,它引用了ruby-1.9.3-p327,这甚至都不是我的RVM安装之一。 我应该删除那个Ruby文件夹吗?它没有找到libruby? 或者还有什么可能是错的? 编辑 – 完全错误 /Users/sashafklein/rails//vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require’: dlopen(/Users/sashafklein/rails//vendor/bundle/ruby/1.9.1/gems/posix-spawn-0.3.6/lib/posix_spawn_ext.bundle, 9): Library not loaded: /Users/sashafklein/.rvm/rubies/ruby-1.9.3-p327/lib/libruby.1.9.1.dylib (LoadError) Referenced from: /Users/sashafklein/rails//vendor/bundle/ruby/1.9.1/gems/posix-spawn-0.3.6/lib/posix_spawn_ext.bundle Reason: image not found – /Users/sashafklein/rails//vendor/bundle/ruby/1.9.1/gems/posix-spawn-0.3.6/lib/posix_spawn_ext.bundle from /Users/sashafklein/rails//vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `block in require’ from /Users/sashafklein/rails//vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency’ from /Users/sashafklein/rails//vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require’ from /Users/sashafklein/rails//vendor/bundle/ruby/1.9.1/gems/posix-spawn-0.3.6/lib/posix/spawn.rb:2:in […]