类变量的范围

在两个B和C类中设置一个类变量@@foo ,其中两个类都不是另一个的子类,但它们都包含一个公共模块A ,似乎分别为B和C创建@@foo , A无法访问: module A; end class B; include A; @@foo = 1 end class C; include A; @@foo = 2 end module A; p @@foo end # => NameError: uninitialized class variable @@foo in A class B; p @@foo end # => 1 class C; p @@foo end # => 2 但是,如果在A分配@@foo […]

VIM逐块插入

我想在VIM中选定的文本块的开头插入一个哈希值(ruby注释)。 我在可视模式下选择了这些行,但是如何对所有行执行相同的操作?

在rails开发中加载类后代

当我在本地进入rails控制台时,我需要能够从控制器中看到所有类后代。 我有这个Api::BaseController ,我的所有Api控制器都inheritance自。 我遇到的问题是当我跳到rails控制台检查哪个Api控制器在后代时,它会变空,直到我打电话给他们。 这可能与开发中的类如何不急于加载有关,并且它们不是在本地缓存的。 sample_app$ rails c Loading development environment (Rails 4.2.0) 2.1.5 :001 > Api::BaseController.descendants => [] 2.1.5 :002 > Api::V1::FoosController => Api::V1::FoosController 2.1.5 :003 > Api::BaseController.descendants => [Api::V1::FoosController] 从这个例子中,你可以看到我第一次在Api::BaseController上调用后代时,它是一个空数组。 在调用其中一个控制器之后,该类将被加载并将显示为后代。 在这种情况下,V1中可能有任意数量的控制器以及V2,V3等…… 作为一个愚蠢的丑陋黑客,我能做到 Dir.glob(“#{Rails.root.join(‘app’, ‘controllers’, ‘api’, ‘v1’)}/**/*.rb”).each(&method(:require_dependency)) 但我不想每次进入控制台都要写。 我也在研究一个gem,绝对不想把这种代码放在我的gem中。 另一种选择是在开发中缓存类,但这会导致一个巨大的问题。 有人有主意吗? 编辑另一个选项是调用Rails.application.eager_load! 。 如果我只能在API文件夹中指定控制器,则此选项可以正常工作。 然后我不必急于加载整个应用程序,而只需要我需要的一小部分控制器。

Lion OSX:如何安装libfreetype.6.dylib?

我正在尝试安装ruby gem,但安装过程已停止,因为: ld: file not found: /usr/local/lib/libfreetype.6.dylib for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 我该如何安装这个库? 我检查了/usr/local/lib/ ,那里缺少库。 编辑: 我跑了找到libfreetype.6.dylib并得到: /Applications/MAMP/Library/lib/libfreetype.6.dylib /Applications/VLC.app/Contents/MacOS/lib/libfreetype.6.dylib /Applications/XAMPP/xamppfiles/lib/libfreetype.6.dylib /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/usr/X11/lib/libfreetype.6.dylib /Applications/calibre.app/Contents/Frameworks/libfreetype.6.dylib /Users/xxxxx/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib /Users/xxxxx/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM /Users/xxxxx/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM/Contents /Users/xxxxx/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM/Contents/Info.plist /Users/xxxxx/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM/Contents/Resources /Users/xxxxx/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM/Contents/Resources/DWARF /Users/xxxxx/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM/Contents/Resources/DWARF/libfreetype.6.dylib /Users/xxxxx/rubydev/sharelock-staging/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib /Users/xxxxx/rubydev/sharelock-staging/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM /Users/xxxxx/rubydev/sharelock-staging/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM/Contents /Users/xxxxx/rubydev/sharelock-staging/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM/Contents/Info.plist /Users/xxxxx/rubydev/sharelock-staging/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM/Contents/Resources /Users/xxxxx/rubydev/sharelock-staging/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM/Contents/Resources/DWARF /Users/v/rubydev/sharelock-staging/magick-installer/freetype-2.4.3/objs/.libs/libfreetype.6.dylib.dSYM/Contents/Resources/DWARF/libfreetype.6.dylib /opt/X11/lib/libfreetype.6.dylib /usr/local/Cellar/freetype/2.4.10/lib/libfreetype.6.dylib 但为什么系统找不到图书馆?

如何在没有外部依赖性的情况下在Ruby中生成图像?

我想在我的Rails应用程序之外使用我自己的数据在Google地图上实现热图。 为此,我需要根据我的数据为每个地图坐标(平铺)生成叠加图像。 我找到了如何使用ImageMagick生成它,但我不想依赖于此,因为我计划在几个地方分发我的应用程序,所以我想只使用Ruby语言。 请注意,我只需要在一个小的透明方块上绘制不同颜色的点。

运行heroku db时无法获取表信息:pull

我有一个在开发和生产中使用postgres的应用程序。 我最近推到了heroku,一切都很好。 我能够从heroku中提取数据库并且没有任何问题地推回来。 然而今天,我试图将数据库从heroku中拉下来,我收到了这个错误 heroku db:pull Loaded Taps v0.3.24 Auto-detected local database: postgres://postgres:a@127.0.0.1/app_development?encoding=utf8 Warning: Data in the database ‘postgres://postgres:a@127.0.0.1/app_development?encoding=utf8’ will be overwritten and will not be recoverable. ! WARNING: Destructive Action ! This command will affect the app: app ! To proceed, type “app” or re-run this command with –confirm app > app Receiving schema […]

什么测试框架用于Rails?

什么unit testing框架用于Rails? 我正在阅读这本书(Pragmatic Programmers Agile Dev.with Rails),它展示了如何在Rails中进行unit testing。 这本书向我展示了默认的rails测试套件( Test::Unit的子类)。 这是Rails社区中使用的主要测试框架吗? 我在做普通ruby时使用RSpec,我也希望能够在Rails中使用它(如果不是太麻烦)?

如果这段代码不是一个笑话,它究竟是如何工作的?

class Tree def initialize*d;@d,=d;end def to_s;@l||@r?”,>”:@d;end def total;(@d.is_a?(Numeric)?@d:0)+(@l?@l.total: 0)+(@r?@r.total: 0);end def insert d alias g instance_variable_get p=lambda{|s,o|d.to_s.send(o,@d.to_s)&& (g(s).nil??instance_variable_set(s,Tree.new(d)):g(s).insert(d))} @d?p[:@l,:]:@d=d end end 有人愿意尝试解释这是做什么的吗? 它似乎是一个问题的答案,我问的代码太聪明了 。 但是,对我来说,判断这只是一个笑话真是太聪明了。 如果不是,我有兴趣知道它是如何工作的,如果有人愿意解释的话。

Ruby自动删除临时文件?

我糊涂了。 这是我的代码: require ‘csv’ require ‘tempfile’ f = Tempfile.new(‘csv’,’/tmp’) f.write ‘just wanna test’ f.close p f.path 如果我打开输出路径,它是空的。 我想这是因为每次ruby会话退出时,TempFile都会自动从文件系统中删除。 但是,如何确切地知道文件何时被删除? 因为我正在考虑使用它在我的rails应用程序中创建临时文件,我担心如果文件在使用之前被删除了。

如何使用rspec存根/模拟对命令行的调用?

我正在尝试从命令行工具测试输出。 如何使用rspec“伪造”命令行调用? 执行以下操作不起作用: it “should call the command line and return ‘text'” do @p = Pig.new @p.should_receive(:run).with(‘my_command_line_tool_call’).and_return(‘result text’) end 如何创建该存根?