当在Ruby中继承类时,单例类也会被继承:
class A
def self.hello
puts "hello"
end
end
class B < A
end
B.hello #=> "hello"
Run Code Online (Sandbox Code Playgroud)
但是对于模块,情况并非如此:
module M
def self.goodbye
puts "goodbye"
end
end
class A
include M
end
A.goodbye #=> NameError
Run Code Online (Sandbox Code Playgroud)
为了解决这个限制,许多人都会诉诸这个丑陋的黑客:
module M
def self.included(c)
c.extend ClassMethods
end
module ClassMethods
def goodbye
puts "goodbye"
end
end
end
Run Code Online (Sandbox Code Playgroud)
好的,我的问题是:这个模块限制背后有理论/概念上的原因吗?还是只是一个实施难度?
在查看C源代码(YARV/MRI)之后,我可以确定存在实施难度(不是不可克服的,但是一个完全相同),但这是唯一的原因吗?这种限制还有其他原因吗?
谢谢
I have three models, each having the following associations:
class Model1 < ActiveRecord::Base
has_many :model2s
has_many :model3s
end
class Model2 < ActiveRecord::Base
belongs_to :model1
has_many :model3s, :through => :model1 # will this work? is there any way around this?
end
class Model3 < ActiveRecord::Base
belongs_to :model1
has_many :model2s, :through => :model1 # will this work? is there any way around this?
end
Run Code Online (Sandbox Code Playgroud)
As you can see in the commented text, I have mentioned what I need.
默认的Ruby Sequel行为是在INFO级别记录所有数据库查询(与在DEBUG级别记录的ActiveRecord不同).我该如何改变?
我想写一个代码美化器,我想用Ruby来做.有人能告诉我一个开始的地方吗?我在网上看过很多代码美化,但我从来没有遇到过如何编写代码的任何教程.对于从未承担任何项目(例如编写编译器,解析器等)的人来说,这是一项非常具有挑战性的任务吗?
(还有其他语言更适合这种任务,不包括C/C++吗?)
我有一个rails 3应用程序(我升级了).它运行在乘客和nginx上,但在我的生产服务器上,它也从设置为"生产"的环境开始.我知道我遗漏了一些非常基本的东西,但我无法弄清楚在环境中设置环境的位置而不是环境.rb.
谢谢你的帮助!
更新:好的,我知道我仍然可以用Rails.env ='production'来做到这一点.
这对我来说似乎是一种老派.你知道一个优雅的方法来配置它可能在Capfile或者那样的吗?
当我gem install rmagick-2.13.1.gem从rmagick-2.13.1.gem所在的目录运行时,我得到一个错误,说它无法构建gem原生扩展,在它下面说
c:/Ruby192/bin/ruby.exe extconf.rb
checking for Ruby version >= 1.8.5 ... yes
Unable to get Imagemagick version
***extconf.rb failed***
Could not create Makefile due to some reason, probably lack
of necessary libraries and/or headers. Check the mkmf.log file
for more details.
Run Code Online (Sandbox Code Playgroud)
据我所知,在http://rmagick.rubyforge.org/install-faq.html#os上阅读常见问题解答的答案时,rmagick应该与ImageMagick Windows Installer捆绑在一起.答案还提到了rmagick-win32.gem.我没有在任何地方见过它.这就是为什么我会假设rmagick-2.13.1.gem是我需要的,因为它是唯一可用的,考虑到常见问题解答引用旧版本的rmagick.所以,我真的很困惑这该死的问题是什么.
我还看了一下mkmf.log文件,我发现的唯一的东西就是
checking for Ruby version >= 1.8.5 ... yes
Run Code Online (Sandbox Code Playgroud)
整件事让我感到困惑.所以,任何帮助都将非常感激.非常感谢提前.
我有一系列uniq号码.像这样:[1,2,3,4,7,8,10,12].它可以是未分类的.我需要的是获得这个数组的间隔:
intervals_for [1,2,3,4,7,8,10,12]
#=> "1-4, 7-8, 10,12"
Run Code Online (Sandbox Code Playgroud)
我有自己的解决方案:
def intervals_for(array)
array.sort!
new_array = []
array.each do |a|
if new_array.last and a == new_array.last.last+1
new_array.last << a
else
new_array << [a]
end
end
new_array.map{|a| a.size > 1 ? "#{a.first}-#{a.last}" : a.first}.join(", ")
end
Run Code Online (Sandbox Code Playgroud)
但我认为这里的某个地方更清洁
我是Ruby的新手.一个简单的例子,我需要的:
class Animal
abstract eat()
class Cat < Animal
eat():
implementation
class Dog < Animal
eat():
implementation
Run Code Online (Sandbox Code Playgroud)
换句话说,所有扩展Animal的类都需要eat()方法.
在JAVA中我只使用一个抽象类,但经过一些研究后我发现许多人不在Ruby中使用它,而建议使用mixin/modules.
但是,我不明白,如果模块可以做的不仅仅是包含一个额外的方法.确切地说,模块是否可以为类必须实现哪些方法设置要求(如果是,可以赞赏一个例子)?
总而言之,在这种情况下我应该使用什么,当我想确定,所有相同类型的类都有特定的方法并以自己的方式实现它们?
我有一个包含多个场景的场景大纲.我希望我的Before钩子只运行一次所以我可以引导我需要针对所有场景运行的ActiveRecord对象.问题是如果我使用
Before do
# my code here
end
Run Code Online (Sandbox Code Playgroud)
这将在每个场景之前执行.反正有没有为整个大纲运行一次?
有人为什么我这样写:
ruby-1.8.7-p302 > a = %w( a b c)
=> ["a", "b", "c"]
ruby-1.8.7-p302 > while (i = a.shift) do; puts i ; end
a
b
c
=> nil
Run Code Online (Sandbox Code Playgroud)
这看起来像是通过一个块来.并不是:
while(i = a.shift) { puts i; }
Run Code Online (Sandbox Code Playgroud)
是因为while语法的"do"只是语法糖而且与块的"do"无关?
ruby ×10
mixins ×2
associations ×1
belongs-to ×1
cucumber ×1
environment ×1
imagemagick ×1
inheritance ×1
logging ×1
module ×1
rmagick ×1
rspec ×1
rubygems ×1
sequel ×1
singleton ×1