From 8760ab19ec17ef80a76a580c36d94a165d8f13c7 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 11 Jan 2023 14:56:47 +0100 Subject: [PATCH] Avoid anonymous eval It makes it hard to locate code when profiling etc. --- lib/mutex_m.rb | 12 +++++------- test/test_mutex_m.rb | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/mutex_m.rb b/lib/mutex_m.rb index 17ec992..4c888d6 100644 --- a/lib/mutex_m.rb +++ b/lib/mutex_m.rb @@ -44,13 +44,11 @@ module Mutex_m Ractor.make_shareable(VERSION) if defined?(Ractor) def Mutex_m.define_aliases(cl) # :nodoc: - cl.module_eval %q{ - alias locked? mu_locked? - alias lock mu_lock - alias unlock mu_unlock - alias try_lock mu_try_lock - alias synchronize mu_synchronize - } + cl.alias_method(:locked?, :mu_locked?) + cl.alias_method(:lock, :mu_lock) + cl.alias_method(:unlock, :mu_unlock) + cl.alias_method(:try_lock, :mu_try_lock) + cl.alias_method(:synchronize, :mu_synchronize) end def Mutex_m.append_features(cl) # :nodoc: diff --git a/test/test_mutex_m.rb b/test/test_mutex_m.rb index e5cfbc8..f938e71 100644 --- a/test/test_mutex_m.rb +++ b/test/test_mutex_m.rb @@ -55,4 +55,25 @@ def initialize def test_initialize_no_args assert NoArgInitializeChild.new end + + def test_alias_extended_object + object = Object.new + object.extend(Mutex_m) + + assert object.respond_to?(:locked?) + assert object.respond_to?(:lock) + assert object.respond_to?(:unlock) + assert object.respond_to?(:try_lock) + assert object.respond_to?(:synchronize) + end + + def test_alias_included_class + object = NoArgInitializeChild.new + + assert object.respond_to?(:locked?) + assert object.respond_to?(:lock) + assert object.respond_to?(:unlock) + assert object.respond_to?(:try_lock) + assert object.respond_to?(:synchronize) + end end