Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to override rand more easily #26

Open
rykov opened this issue Mar 21, 2023 · 0 comments
Open

Ability to override rand more easily #26

rykov opened this issue Mar 21, 2023 · 0 comments

Comments

@rykov
Copy link

rykov commented Mar 21, 2023

I'm trying to override rand in Spicy::Seek to allow for the flexibility of making the function deterministic based on an incoming seed. Before submitting a PR, I wanted to propose a couple of ways to implement this.

  1. The easiest is to allow the options hash to be forwarded to rand. Then I can extend the classes and override rand with my own custom implementation:
  def seek(opts = {}, &found)
  ...
-      index = rand(rand_min, rand_max)
+      index = rand(rand_min, rand_max, opts)
  ...
  end

- def rand(low, high)
+ def rand(low, high, opts = {})
    range = high - low + 1
    low + SecureRandom.random_number(range)
  end
  1. Allow passing a lambda as :rand option:
  def seek(opts = {}, &found)
  ...
-      index = rand(rand_min, rand_max)
+      index = opts[:rand] ? opts[:rand].call(rand_min, rand_max) : rand(rand_min, rand_max)
  1. Any suggestions how I can make this work without otherwise.

Let's discuss below, and I can submit a PR with the preferred implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant