Ruby, Generate a random hex color

后端 未结 5 577
眼角桃花
眼角桃花 2020-12-23 02:09

How can I generate a random hex color with ruby?

5条回答
  •  爱一瞬间的悲伤
    2020-12-23 02:18

    SecureRandom.hex(3)
    #=> "fef912"
    

    The SecureRandom module is part of Ruby's standard library

    require 'securerandom'
    

    It's autoloaded in Rails, but if you're using Rails 3.0 or lower, you'll need to use

    ActiveSupport::SecureRandom.hex(3)
    

提交回复
热议问题