ErrorMessages
I just wrote something to get the libc descriptions of Ruby Errno.
Below is something else.. just some code to show some stuff.
p @names
# => {"Olle"=>"11", "Pelle"=>"6", "Kalle"=>"2", "Pellegrino"=>"0"}
BEGIN {
NL, CM = "\n", ','
module AmpArgs
refine Symbol do
def call(*args, &block) = ( ->(caller, *rest) {
caller.send(self, *rest, *args, &block) } )
end
end
using AmpArgs
@names = <<~NAMES.split(NL).map(&:split.(CM)).to_h
Olle,3
Pelle,6
Kalle,2
Olle,11
Pellegrino,0
NAMES
}
The BEGIN block is run first. That is to read the HEREdoc into a hash.
A HEREdoc takes methods.. so they can be applied directly on the <<~NAMES
.
The module is allowing ampersand mapping to take arguments.
In this case: map(&:split.(CM))
.
In the module the Symbol class is refined.. and later activated with using
.
I make names
an instance-variable.. to not have it exist just in the BEGIN context.
In my code, the HEREdoc is not five names, but a long list of libc error names and the corresponding short description. I match this against Errno.constants
and add the Ruby error codes Errno::ERROR_NAME::Errno
.