基本上我想在每次提交后获取存储库中的代码行数.
我发现的唯一(非常糟糕)方法是使用git filter-branch运行,然后运行每个提交wc -l *的脚本git reset --hardwc -l
为了使它更清楚一点,当工具运行时,它将输出第一次提交的代码行,然后输出第二次提交的代码行,依此类推.这就是我希望工具输出的内容(作为示例):
me@something:~/$ gitsloc --branch master
10
48
153
450
1734
1542
Run Code Online (Sandbox Code Playgroud)
我玩过ruby'git'库,但是我发现最接近的是.lines()在diff上使用该方法,这似乎应该给出添加的行(但不是:例如当你删除行时它会返回0)
require 'rubygems'
require 'git'
total = 0
g = Git.open(working_dir = '/Users/dbr/Desktop/code_projects/tvdb_api')
last = nil
g.log.each do |cur|
diff = g.diff(last, cur)
total = total + diff.lines
puts total
last = cur
end
Run Code Online (Sandbox Code Playgroud) 我有一个数组,我想制作一个哈希,所以我可以快速问"数组中的X是什么?".
在perl中,有一种简单(快速)的方法:
my @array = qw( 1 2 3 );
my %hash;
@hash{@array} = undef;
Run Code Online (Sandbox Code Playgroud)
这会生成一个如下所示的哈希:
{
1 => undef,
2 => undef,
3 => undef,
}
Run Code Online (Sandbox Code Playgroud)
我在Ruby中提出的最好的是:
array = [1, 2, 3]
hash = Hash[array.map {|x| [x, nil]}]
Run Code Online (Sandbox Code Playgroud)
这使:
{1=>nil, 2=>nil, 3=>nil}
Run Code Online (Sandbox Code Playgroud)
有更好的Ruby方式吗?
不,Array.include?不是个好主意.它很慢.它在O(n)中进行查询而不是O(1).为简洁起见,我的示例数组有三个元素; 假设实际的有一百万个元素.我们做一点基准测试:
#!/usr/bin/ruby -w
require 'benchmark'
array = (1..1_000_000).to_a
hash = Hash[array.map {|x| [x, nil]}]
Benchmark.bm(15) do |x|
x.report("Array.include?") { 1000.times { array.include?(500_000) } }
x.report("Hash.include?") { 1000.times { hash.include?(500_000) } }
end …Run Code Online (Sandbox Code Playgroud) 我知道您可以通过键入来查看所有可能的rake任务
rake -T
Run Code Online (Sandbox Code Playgroud)
但我需要知道一项任务到底是做什么的.从输出中,我如何找到实际具有任务的源文件?例如,我正在尝试查找db:schema:dump任务的源代码.
当我使用本机sort方法对数组进行排序时,Ruby使用哪种算法?
它是数据依赖的,即,如果数据很小,它使用X算法,否则它使用Y算法?
这是稳定的吗?平均时间复杂度是多少?
有没有办法通过Gem模块检查当前是否安装了某些gem?从ruby代码,而不是通过执行'宝石列表'...
澄清 - 我不想加载库.我只是想检查它是否可用,所以所有的rescue LoadError解决方案都没有帮助我.此外,我不在乎宝石本身是否有效,只是它是否已安装.
情况:我有多个类,每个类应该包含一个带有配置哈希的变量; 每个类的不同哈希,但对于类的所有实例都是相同的.
起初,我试过这样的
class A
def self.init config
@@config = config
end
def config
@@config
end
end
class B < A; end
class C < A; end
Run Code Online (Sandbox Code Playgroud)
但很快就注意到它不会那样工作,因为@@ config是在A的上下文中保存,而不是B或C,因此:
B.init "bar"
p B.new.config # => "bar"
p C.new.config # => "bar" - which would be nil if B had it's own @@config
C.init "foo"
p B.new.config # => "foo" - which would still be "bar" if C had it's own @@config
p C.new.config # => "foo"
Run Code Online (Sandbox Code Playgroud)
我想过像这样使用它:
modules = [B, …Run Code Online (Sandbox Code Playgroud) 我有一个带有工厂方法的脚本,我希望返回某个类的不同实现,具体取决于脚本是从JRuby还是Ruby运行.任何人都有任何关于如何从我的脚本中区分出来的想法?
我的一些初步想法是:
如果失败,尝试"包含Java"并回退到Ruby实现.此方法不起作用.无论我的开始/救援/结束如何,Ruby都足够聪明.
用进程ID做一些傻瓜.我宁愿避免这种情况,因为它总是像黑客一样.
我正在尝试为脚本接收的命令行参数指定行为,以确保所有验证都通过.我的一些命令行参数将导致abort或被exit调用,因为提供的参数缺失或不正确.
我正在尝试这样不起作用的东西:
# something_spec.rb
require 'something'
describe Something do
before do
Kernel.stub!(:exit)
end
it "should exit cleanly when -h is used" do
s = Something.new
Kernel.should_receive(:exit)
s.process_arguments(["-h"])
end
end
Run Code Online (Sandbox Code Playgroud)
该exit方法干净利落地阻止RSpec验证测试(我得到"SystemExit:exit").
我也尝试过,mock(Kernel)但是这也没有按照我的意愿工作(我没有看到任何明显的区别,但这可能是因为我不确定如何模拟内核并确保在我的内核中使用了模拟的内核Something类).
我正在尝试为此声明构建一个规范.'puts'很容易
print "'#{@file}' doesn't exist: Create Empty File (y/n)?"
Run Code Online (Sandbox Code Playgroud) 假设我有这个模型:
class Conversation < ActiveRecord::Base
enum status: [ :active, :archived ]
end
Run Code Online (Sandbox Code Playgroud)
如何在不使用枚举的数值或不必迭代每个对话的情况下找到所有活动对话?
我试过了Conversation.where(status: :active),但没有产生任何结果.
想到的唯一解决方案是迭代所有对话并选择活动对话,但它看起来不是一个好的解决方案.
Conversation.all.select {|conversation| conversation.active? }
Run Code Online (Sandbox Code Playgroud)
我能做些什么吗?
ruby ruby-on-rails ruby-on-rails-4 rails-activerecord ruby-on-rails-4.1