Module: OpEsc

Defined in:
lib/standard/facets/opesc.rb

Overview

OpEsc

NOTE: As of v3.0 of Facets these have been renamed:

op_plus_self   -> op_plus
op_minus_self  -> op_minus
op_plus        -> op_add
op_minus       -> op_sub
op_case_eq     -> op_case

TODO: In the future we might also rename:

op_lshift      -> op_push
op_rshift      -> op_pull
op_store       -> op_index
op_fetch       -> op_index  (b/c [] and []= in same file)

Constant Summary collapse

OPERATORS =
%w{ +@ -@ + - ** * / % ~ <=> << >> < > === == =~ <= >= | & ^ []= [] }
OPERATORS_REGEXP =
Regexp.new( '(' << OPERATORS.collect{ |k| Regexp.escape(k) }.join('|') << ')' )
OPERATORS_ESC_TABLE =
{
    "+@"   => "op_plus",
    "-@"   => "op_minus",
    "+"    => "op_add",
    "-"    => "op_sub",
    "**"   => "op_pow",
    "*"    => "op_mul",
    "/"    => "op_div",
    "%"    => "op_mod",
    "~"    => "op_tilde",
    "<=>"  => "op_cmp",
    "<<"   => "op_lshift",  #push?
    ">>"   => "op_rshift",  #pull?
    "<"    => "op_lt",
    ">"    => "op_gt",
    "==="  => "op_case",
    "=="   => "op_equal",
    "=~"   => "op_apply",
    "<="   => "op_lt_eq",
    ">="   => "op_gt_eq",
    "|"    => "op_or",
    "&"    => "op_and",
    "^"    => "op_xor",
    "[]="  => "op_store",
    "[]"   => "op_fetch"
}

Class Method Summary collapse

Class Method Details

.escape(str) ⇒ Object

Applies operator escape’s according to OPERATORS_ESCAPE_TABLE.

OpEsc.escape('-') #=> "op_sub"

CREDIT: Trans



55
56
57
# File 'lib/standard/facets/opesc.rb', line 55

def self.escape(str)
  str.to_s.gsub(OPERATORS_REGEXP){ OPERATORS_ESC_TABLE[$1] }
end

.method_to_filename(name) ⇒ Object



60
61
62
63
64
# File 'lib/standard/facets/opesc.rb', line 60

def self.method_to_filename(name)
  fname = escape(name)
  fname = fname[0...-1] if fname =~ /[\!\=\?]$/
  fname
end