Skip to content

Commit

Permalink
Avoid anonymous eval
Browse files Browse the repository at this point in the history
It makes it hard to locate code when profiling etc.
  • Loading branch information
byroot authored and hsbt committed Jan 12, 2023
1 parent 99f0b16 commit 8760ab1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
12 changes: 5 additions & 7 deletions lib/mutex_m.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions test/test_mutex_m.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8760ab1

Please sign in to comment.