Crystal Wiki[1] says an alternative to Ruby alias_method
is using previous_def
. From what I can see, this doesn’t do much, compared to Ruby style. I’ve used and seen alias_method
both in libraries (for deprecation support), and also in application types for execution of previous method definition (in specific cases, ie. shorthands).
Using alias_method
is pretty simple in Ruby[2]. Just alias to previous definition, similar to what Crystal offers (ie. in inheritance).
Playground: https://repl.it/repls/SpitefulUnevenGuiltware
class User
def full_name
puts "Nur Yubr"
end
# Using `alias_method` to run previous declar
alias_method :name, :full_name
end
User.new.name
Using perf_def
overwrites previous method declaration, as per the Wiki. This works well if inheritance is in use. But otherwise, we can’t alias to previous method definition.
Playground: https://play.crystal-lang.org/#/r/6btf
class User
def full_name
puts "Nur Yubr"
end
# doesn't make much sense to use previous_def
# => previous_def
# `alias` is not usable in this case
# alias name = full_name
# hacky alternative
def name
full_name
end
end
User.new.name
Any points? @Sija pointed asking @r00ster91, maybe he can share some detail. Anyway, looking forward to see alternatives.