如何按每个int的第一个数字对一片int进行排序?
我正在尝试编写自己的自定义排序:
type ByFirstDigit []int
func (s ByFirstDigit) Len() int {
return len(s)
}
func (s ByFirstDigit) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByFirstDigit) Less(i, j int) bool {
return s[i][0] < s[j][0]
}
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
s [j] [0](int类型不支持索引)
我有一个名为的文件example.go,另一个名为的测试文件example_test.go,它们都在同一个程序包中。我想测试一些未导出的功能example.go
运行测试时,未导出的函数在中未定义example_test.go。我想知道在同一程序包中的测试文件中测试未导出函数的最佳约定是什么?
例1:
class Dog
def self.class_method
:another_way_to_write_class_methods
end
end
def test_you_can_use_self_instead_of_an_explicit_reference_to_dog
assert_equal :another_way_to_write_class_methods, Dog.class_method
end
Run Code Online (Sandbox Code Playgroud)
例2:
class Dog
class << self
def another_class_method
:still_another_way
end
end
end
def test_heres_still_another_way_to_write_class_methods
assert_equal :still_another_way, Dog.another_class_method
end
Run Code Online (Sandbox Code Playgroud)
我可以知道在Ruby中编写类方法的哪种方式是首选的,为什么?是否存在一种优先于另一种情况的情况?
似乎RubyMine IDE在看到负面条件语句时发出警告.我想知道为什么使用否定条件语句是坏的?它纯粹是因为可读性吗?
例如,在此代码中:
class Complement
def self.of_dna dna_strand
dna_array = dna_strand.chars
dna_complement = ['']
dna_structure = ['C', 'G', 'T', 'A']
dna_array.each do |strand|
unless dna_structure.include? strand
return ''
end
case strand
when "C"
dna_complement << "G"
when "G"
dna_complement << "C"
when "T"
dna_complement << "A"
when "A"
dna_complement << "U"
end
end
dna_complement.join('')
end
end
Run Code Online (Sandbox Code Playgroud)
我想知道什么是之间的不同unless dna_structure.include? strand而if !(dna_strucutre.include?)在这种情况下?
我尝试过,SecureRandom.random_number(9**6)但有时返回5,有时返回6。我希望它的长度始终为6。我也更喜欢它的格式,SecureRandom.random_number(9**6)而无需使用类似的语法,6.times.map以便在控制器测试中更容易进行打桩。
ruby ×3
go ×2
syntax ×2
class ×1
class-method ×1
conditional ×1
indexing ×1
int ×1
methods ×1
rspec-rails ×1
sorting ×1
unit-testing ×1