Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4636

Prepend Kernel module function globally

$
0
0

I want to prepend Kernel.rand like this:

# I try something likemod = Module.new do  def rand(*args)    p "do something"    super(*args)  endendKernel.prepend(mod)
# And I expect this behaviourKernel.rand            #> prints "do something" and returns random numberrand                   #> prints "do something" and returns random numberObject.new.send(:rand) #> prints "do something" and returns random number 

Unfortunately, the code above does not work as I want to. Prepending Kernel.singleton_class does not work too

It's not required to use prepend feature, any suggestiion that will help to achieve the desired behaviour is welcome


Viewing all articles
Browse latest Browse all 4636

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>