Ruby: storing a value at inclusion time - ruby

In a Ruby class, I'd like to store the value of a variable at the time it includes a given module. Below is a contrived example:
module M
def self.included(base)
base.class_eval do
##inclusion_time = Time.now
def included_at
##inclusion_time
end
end
end
end
class A
include M
end
sleep 3
class B
include M
end
sleep 3
class C
include M
end
a = A.new
b = B.new
c = C.new
puts a.included_at
puts b.included_at
puts c.included_at
I've tried this any number of ways (attr_accessor, set_constant, etc,) but the end result is always the same. All of the classes have whatever value was set last.
How do I work around this?

module M
def self.included _
#inclusion_time = Time.now
end
def included_at
self.class.instance_eval{#inclusion_time}
end
end

Related

Identifying a Ruby module method invoked by super?

Ruby 2.7+.
I have methods in a couple of modules that are mixed in, and are invoked with super. Each of the module methods invokes super in turn, so all methods by that name in the mixed-in modules are invoked, although perhaps not in a deterministic order.
My question is: Can a method tell programmatically (as opposed to hard-coding) from what module it's been mixed in?
module A
def initialize(*args, **kwargs)
puts("something magic")
end
end
module B
def initialize(*args, **kwargs)
puts("something magic")
end
end
class C
include A
include B
def initialize(*args, **kwargs)
puts("actual #{class.name} initialize")
super
end
end
When run, this will print three lines. What I'm seeking is something like class.name, specific to each module, that identifies the module that supplied the initialize method that's running. The "something magic* strings would be replaced with this actual magic. 🙂
Thanks!
My first attempt was to call super_method to get all the initializers up the stack:
module A
def initialize
A # return for comparison
end
end
module B
def initialize
B
end
end
class C
include A
include B
def initialize
C
end
def super_initializers
init = method(:initialize)
while init
print init.call, " == " # get hardcoded module from `initialize`
p init.owner # get the module dynamically
init = init.super_method # keep getting the super method
end
end
end
>> C.new.super_initializers
C == C
B == B
A == A
== BasicObject
Second idea is to use Module.nesting, I think this is what you're looking for:
module A
def initialize
# i, o, m = method(:initialize), [], Module.nesting[0]
# while i; o << i.owner; i = i.super_method; end
# print "prev "; p o[o.index(m)-1] # previous super
puts "A == #{Module.nesting[0]}"
# print "next "; p o[o.index(m)+1] # next super
super
end
end
module B
def initialize
puts "B == #{Module.nesting[0]}"
super
end
end
# add a class
class Y
def initialize
puts "Y == #{Module.nesting[0]}"
super
end
end
# add some nesting
module X
class Z < Y
def initialize
puts "Z == #{Module.nesting[0]}"
super
end
end
end
class C < X::Z
include A
include B
def initialize
puts "C == #{Module.nesting[0]}"
super
end
end
>> C.new
C == C
B == B
A == A
Z == X::Z
Y == Y
Actually, never thought about that this could be useful, but it works:
def super_trace m
while m;
p m.owner; m = m.super_method
end
end
>> super_trace User.new.method(:save)
ActiveRecord::Suppressor
ActiveRecord::Transactions
ActiveRecord::Validations
ActiveRecord::Persistence
https://rubyapi.org/3.1/o/module#method-c-nesting
https://rubyapi.org/3.1/o/method#method-i-super_method

How to "magically" add code to all public class methods in ruby?

I would like to be able to insert some code at the beginning and at the end of methods in my class. I would like to avoid repetition as well.
I found this answer helpful, however it doesn't help with the repetition.
class MyClass
def initialize
[:a, :b].each{ |method| add_code(method) }
end
def a
sleep 1
"returning from a"
end
def b
sleep 1
"returning from b"
end
private
def elapsed
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
block_value = yield
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
puts "elapsed: #{finish - start} seconds, block_value: #{block_value}."
block_value
end
def add_code(meth)
meth = meth.to_sym
self.singleton_class.send(:alias_method, "old_#{meth}".to_sym, meth)
self.singleton_class.send(:define_method, meth) do
elapsed do
send("old_#{meth}".to_sym)
end
end
end
end
The above does work, but what would be a more elegant solution? I would love to be able to, for example, put attr_add_code at the beginning of the class definition and list the methods I want the code added to, or perhaps even specify that I want it added to all public methods.
Note: The self.singleton_class is just a workaround since I am adding code during the initialisation.
If by repetition you mean the listing of methods you want to instrument, then you can do something like:
module Measure
def self.prepended(base)
method_names = base.instance_methods(false)
base.instance_eval do
method_names.each do |method_name|
alias_method "__#{method_name}_without_timing", method_name
define_method(method_name) do
t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
public_send("__#{method_name}_without_timing")
t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
puts "Method #{method_name} took #{t2 - t1}"
end
end
end
end
end
class Foo
def a
puts "a"
sleep(1)
end
def b
puts "b"
sleep(2)
end
end
Foo.prepend(Measure)
foo = Foo.new
foo.a
foo.b
# => a
# => Method a took 1.0052679998334497
# => b
# => Method b took 2.0026899999938905
Main change is that i use prepend and inside the prepended callback you can find the list of methods defined on the class with instance_methods(false), the falseparameter indicating that ancestors should not be considered.
Instead of using method aliasing, which in my opinion is something of the past since the introduction of Module#prepend, we can prepend an anonymous module that has a method for each instance method of the class to be measured. This will cause calling MyClass#a to invoke the method in this anonymous module, which measures the time and simply resorts to super to invoke the actual MyClass#a implementation.
def measure(klass)
mod = Module.new do
klass.instance_methods(false).each do |method|
define_method(method) do |*args, &blk|
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
value = super(*args, &blk)
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
puts "elapsed: #{finish - start} seconds, value: #{value}."
value
end
end
end
klass.prepend(mod)
end
Alternatively, you can use class_eval, which is also faster and allows you to just call super without specifying any arguments to forward all arguments from the method call, which isn't possible with define_method.
def measure(klass)
mod = Module.new do
klass.instance_methods(false).each do |method|
class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{method}(*)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
value = super
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
puts "elapsed: \#{finish - start} seconds, value: \#{value}."
value
end
CODE
end
end
klass.prepend(mod)
end
To use this, simply do:
measure(MyClass)
It looks like you're trying to do some benchmarking. Have you checked out the benchmark library? It's in the standard library.
require 'benchmark'
puts Benchmark.measure { MyClass.new.a }
puts Benchmark.measure { MyClass.new.b }
Another possibility would be to create a wrapper class like so:
class Measure < BasicObject
def initialize(target)
#target = target
end
def method_missing(name, *args)
t1 = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
target.public_send(name, *args)
t2 = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
::Kernel.puts "Method #{name} took #{t2 - t1}"
end
def respond_to_missing?(*args)
target.respond_to?(*args)
end
private
attr_reader :target
end
foo = Measure.new(Foo.new)
foo.a
foo.b

How to know from what module user was called

I have 2 modules and 1 class:
module B
def hi
say 'hi'
end
end
module C
def say(message)
puts "#{message} from ???"
end
end
class A
include B
include C
end
A.new.hi
#=> hi from ???"
How i can get message as hi from B?
You could use caller_locations to determine the calling method's name, and use that information to retrieve the method's owner:
module C
def say(message)
method_name = caller_locations(1, 1)[0].base_label
method_owner = method(method_name).owner
puts "#{message} from #{method_owner}"
end
end
But this is very brittle. It would be much easier to simply pass the calling module, e.g.:
module B
def hi
say 'hi', B
end
end
module C
def say(message, mod)
puts "#{message} from #{mod}"
end
end
You can use Kenel#__method__ with Method#owner:
module C
def say(message)
puts "#{message} from #{method(__method__).owner }"
end
end
class A
include C
end
A.new.say('hello')
#=> hello from C
Kenel#__method__:
Returns the name at the definition of the current method as a Symbol.
If called outside of a method, it returns nil.
Method#owner:
Returns the class or module that defines the method.

Invoking 'initialize' of several included modules in Ruby

If I include a module into a class, which has initialize defined, I can call it using super:
module M
def initialize(x)
#m = x
end
end
class MyClass
def initialize
super(3)
end
def val
#m
end
end
MyClass.new.val
# => 3
But how do I code this, if I have several modules, and maybe also a parent class?
class Parent
def initialize(x)
#p = x
end
end
module M
def initialize(x)
#m = x
end
end
module N
def initialize(x)
#n = x
end
end
class MyClass < Parent
include M
include N
def initialize
# ???? How to initialize here?
end
def val
[#m,#n,#p]
end
end
I guess that super(100) within MyClass::initialize would set the variable #n, because N is the "most recent" ancestor, but how do I call the initialize methods in M and Parent?
Take a look at this blog post (http://stdout.koraktor.de/blog/2010/10/13/ruby-calling-super-constructors-from-multiple-included-modules/). It explains how do you use the initialize from different included modules.

how to get (and set) variables inside a ruby class?

I update this question to better reflect what I have problems to grasp. The example below kind of work but how can I access the Sub class then I have defined it inside the Base class? Should it not be better to do the call outside the class? If so how do I do that? The second question I have in this example is how to grab values so I can use them in another class. Here I store the values in an array that I later need to unpack in another class. Should I not be able to use a proc for this?
Basically what I want to do is to sort the methods into two different classes depending on if they are nested or not.
class Sub
def initialize(base_class_method)
#base_class_method = base_class_method
#sub_methods = []
end
# omitted code here
def base_class_method
#base_class_method
end
def sub_actions(method)
#sub_methods << method
end
def return_sub_methods
#sub_methods
end
def method_missing(sub_method, &block)
if sub_method
sub_method
else
super
end
end
end
class Base
def initialize
#base_methods = []
end
# omitted code here
def base_actions(method)
#base_methods << method
end
def return_base_methods
#base_methods
end
def method_missing(method, &block)
if block_given?
Sub.new(method).instance_eval(&block)
elsif method
base_actions(method)
else
super
end
end
end
base = Base.new
base.instance_eval do
something1
something_with_a_block do
something_inside_block1_1
something_inside_block1_2
end
something2
something_with_a_block2_2 do
something_inside_block2_1
end
end
p base.return_base_methods #=> [:something1, :something2] works!
You can do something like this.
class Test
# reserved method to instantiate object
def initialize(a,b,c)
#a = a
#b = b
#c = c
end
# getters
def a
#a
end
def b
#b
end
def c
#c
end
def abc
[#a, #b, #c] # returns an array
end
# setters
def a=(var)
#a = var
end
def b=(var)
#b = var
end
def c=(var)
#c = var
end
# set values all at once
def update(a, b, c)
#a = a
#b = b
#c = c
end
end
z = Test.new('something','something','something')
z.update('something!','nothing!',"a thing!")
z.a
z.b
z.c
z.a = 'wow, new value!'

Resources