Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

To make this discussion really useful: what's the easiest way for a Ruby application to make SecureRandom use /dev/urandom, without waiting for Ruby-core to be convinced?


  require "securerandom"

  module SecureRandom
    F = File.open("/dev/urandom")

    def self.random_bytes(n = nil)
      F.read(n || 16)
    end
  end


Why not just make the argument default to 16? Is something non-obvious going on?


The caller may pass nil explicitly.


I don't understand. You may still pass nil explicitly if the argument defaults to 16?


Yes:

  def f(n = 16)
    n
  end

  p f # => 16
  p f(nil) # => nil




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: