user21082481
user21082481

Reputation:

Do something x times per second

I've been given an interesting ruby question for a technical test. I did not find an answer in the time given, but i think it was interesting and would like someone else's take on this. How would you write code to do:

2.times.each_second { p 'works' }

I tried extending the enumerator class, with a per_second method. The per_second method just sleeps for the correct number of milliseconds. But i was stuck trying to pass the "2" argument to the per_second method in an elegant fashion. Maybe using the length of the enumerator returned from 2.times?

Upvotes: 3

Views: 290

Answers (3)

engineersmnky
engineersmnky

Reputation: 29308

Here is another option using a simple object

obj = Object.new 

def obj.each_second(times: 1, duration: Float::INFINITY)
  return to_enum(__method__, times: times, duration: duration) unless block_given?
  i = 0
  until i >= duration * times do
    i += 1 
    yield(i)
    sleep(1.0/times)
  end
end 

enum = obj.each_second(times: 5, duration: 3)

enum.each { |n|  puts "#{n} Works"}
# 1 Works
# 2 Works
# 3 Works
# 4 Works
# 5 Works
# 6 Works
# 7 Works
# 8 Works
# 9 Works
# 10 Works
# 11 Works
# 12 Works
# 13 Works
# 14 Works
# 15 Works
#=> nil

You can specify the number of times to execute the block per second and the duration of the execution in seconds. (Defaults to 1 iteration per second indefinitely)

Note: This does not take into account the actual blocking execution time of the block itself so the number of iterations per second is not guaranteed and is likely to be less than the specified number.

Upvotes: 0

Silvio Mayolo
Silvio Mayolo

Reputation: 70257

Using the magic of modern Ruby, we can write a refinement to Integer that augments the times method, but only in scopes that opt into this refinement behavior.

module IntegerExt

  # Define a refinement of the Integer type. This is like
  # monkeypatching but is local to modules or files that opt in to the
  # change.
  refine Integer do
    # Re-write the times function.
    def times(&block)
      if block
        # If a block was provided, then do the "normal" thing.
        super(&block)
      else
        # Otherwise, get an enumerator and augment its singleton class
        # with PerSecondEnumerator.
        super().extend PerSecondEnumerator
      end
    end
  end

  # This module provides the per_second method on enumerators that
  # need it.
  module PerSecondEnumerator
    def per_second(&block)
      loop do
        sleep(1.0/self.size)
        block.call
      end
    end
  end

end

# If a module wishes to have this functionality, it needs to opt in
# with this 'using' statement. The per_second syntax only works on
# objects constructed within the lexical scope of this refinement.
using IntegerExt

puts 2.times.inspect # Still works
2.times.per_second { p 'works' }

This deals with a couple of the concerns of Aria's answer. We're no longer globally monkey-patching a built-in class. We only modify the class in the lexical scope of any module that wishes to see our changes. Further, since we've refined times rather than Enumerator directly, our per_second only works on enumerators constructed via times, so it's impossible to do something nonsensical like [1, 2, 3, 4, 5].per_second { ... }.

Upvotes: 4

Aria
Aria

Reputation: 104

You could do it like that:

Enumerator.define_method(:per_second) do |&block|
  loop do
    sleep(1.0/self.size)
    block.call
  end
end

3.times.per_second { puts "works" }

But here are some warnings:

  1. It's not recommended to expand a Ruby base class like Enumerator, Integer, etc.

  2. Like someone said in a comment on your post, it's not good to use size, since you can use a string or array instead of a integer.

Upvotes: 3

Related Questions