我有以下Ruby代码:
module MyModule
class MyClass
def self.my_method
end
end
end
Run Code Online (Sandbox Code Playgroud)
要打电话my_method,我进入MyModule::MyClass.my_method.我想my_method在模块本身上写一个包装器:
MyModule.my_method
Run Code Online (Sandbox Code Playgroud)
这可能吗?
def plural(value, string)
"#{value} #{value.abs == 1 ? string.singularize : string.pluralize}"
end
Run Code Online (Sandbox Code Playgroud)
如果没有,这个方法的简短名称是什么?
有没有办法在不使用Cucumber的情况下使用Rspec进行集成测试?我更喜欢使用普通的旧Webrat.谢谢.
While monkey-patching a module from a Rails engine, we found that if you prepend a module B to another module A, the prepended module B won't be added to the ancestors of classes that have already included module A prior to the prepend. To illustrate:
module A
def a
puts 'a'
end
end
class B
include A
end
module C
def a
puts 'c'
end
A.prepend self
end
class D
include A
end
B.new.a # prints: a
D.new.a # …Run Code Online (Sandbox Code Playgroud) Ruby似乎没有像这样定义受保护/私有块的工具:
protected do
def method
end
end
Run Code Online (Sandbox Code Playgroud)
这比较好
protected
def method
end
public
Run Code Online (Sandbox Code Playgroud)
你可能会忘记在受保护的方法后"公开".
似乎可以使用元编程实现这一点.有什么想法?
我正在使用paperclip gem将文件附加到模型.使用Paperclip上载文件时,仅在保存模型时保存文件.因此,如果模型无效,则不保存上载的文件.有没有办法临时保存上传的文件,这样如果模型无效,用户不必上传同一个文件?
我有一个基于url参数搜索记录的操作.该动作的URL看起来像这样:
http://domain.com/records/filter/<filtercode>
Run Code Online (Sandbox Code Playgroud)
如果用户输入了错误的过滤代码,我想申请
我知道某些Rails错误,如ActiveRecord :: NotFound和ActionController:RoutingError将在生产环境中呈现404.所以我喜欢做的是当用户在网址中输入无效的过滤码时引发此错误.
现在,我的问题是:在这种情况下提出的理想错误是什么?网络中是否有Rails错误/异常列表?
(免责声明:我之前发布了一个类似的问题:在ruby脚本中,如何让git打开它的消息编辑器.我决定将此作为一个单独的问题发布,因为我认为这个问题更通用,因此可能更适用于其他程序员但是,我保留了与git相关的问题,因为我不确定这里的答案是否也适用于那里.如果有违规的规则,请告诉我)
我目前正在开发一个命令行ruby gem,它可以自动化https://gist.github.com/jbenet/ee6c9ac48068889b0912中讨论的"rebase + no-ff merge"工作流程.您可以在https://github.com/gsmendoza/git_pretty_accept/tree/git_pretty_accept找到此gem的WIP代码.宝石会做这样的事情:
`vi some_temp_file.txt`
`git co master`
`git pull`
`git co pull_request`
`git rebase master`
`git co master`
merge_message = File.read('some_temp_file.txt')
`git merge --message "#{merge_message}" --no-ff pull_request`
`git push`
`git branch -d pull_request`
`git push origin:pull_request`
Run Code Online (Sandbox Code Playgroud)
当我尝试通过ruby运行这些git命令时,vi some_temp_file.txt不会像我希望的那样打开文本编辑器.相反,我看到一个警告"Vim:警告:输出不是终端",脚本只是挂起.
有任何想法吗?