小编Jef*_*ong的帖子

获取int的第一个数字

如何按每个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类型不支持索引)

sorting indexing int go

4
推荐指数
1
解决办法
821
查看次数

在Go中测试未导出的功能

我有一个名为的文件example.go,另一个名为的测试文件example_test.go,它们都在同一个程序包中。我想测试一些未导出的功能example.go

运行测试时,未导出的函数在中未定义example_test.go。我想知道在同一程序包中的测试文件中测试未导出函数的最佳约定是什么?

unit-testing go private-methods

3
推荐指数
2
解决办法
4651
查看次数

Ruby中创建类方法的不同方法

例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中编写类方法的哪种方式是首选的,为什么?是否存在一种优先于另一种情况的情况?

ruby methods syntax class class-method

2
推荐指数
1
解决办法
536
查看次数

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? strandif !(dna_strucutre.include?)在这种情况下?

ruby syntax conditional

2
推荐指数
1
解决办法
741
查看次数

在Ruby中使用SecureRandom生成长度为6的随机数

我尝试过,SecureRandom.random_number(9**6)但有时返回5,有时返回6。我希望它的长度始终为6。我也更喜欢它的格式,SecureRandom.random_number(9**6)而无需使用类似的语法,6.times.map以便在控制器测试中更容易进行打桩。

ruby rspec-rails ruby-on-rails-5 secure-random

2
推荐指数
2
解决办法
1634
查看次数