module Translate # doctest: Translate should give the pre method for a normal method # >> Translate.method_pre(:method) # => :method_pre # # doctest: Translate should give the pre method for a question method # >> Translate.method_pre(:method?) # => :method_pre? # # doctest: Translate should give the pre method for an exclamation method # >> Translate.method_pre(:method!) # => :method_pre! # # doctest: Translate should give the pre method for an assignment method # >> Translate.method_pre(:method=) # => :method_pre= # # doctest: Translate should give the pre method for an operator # >> Translate.method_pre(:+) # => :op_plus_pre def self.method_pre(method) method_with_suffix(method, :pre) end # doctest: Translate should give the post method for a normal method # >> Translate.method_post(:method) # => :method_post # # doctest: Translate should give the post method for a question method # >> Translate.method_post(:method?) # => :method_post? # # doctest: Translate should give the post method for an exclamation method # >> Translate.method_post(:method!) # => :method_post! # # doctest: Translate should give the post method for an assignment method # >> Translate.method_post(:method=) # => :method_post= # # doctest: Translate should give the post method for an operator # >> Translate.method_post(:+) # => :op_plus_post def self.method_post(method) method_with_suffix(method, :post) end private def self.method_with_suffix(method, type) operator = OPERATOR[method] suffix = '_' + type.to_s if operator.nil? method_string = method.to_s length = method_string.length head = method_string[0...length-1] tail = method_string[length-1...length] if ['?', '!', '='].include?(tail) (head + suffix + tail).to_sym else (method_string + suffix).to_sym end else ('op_' + operator.to_s + suffix).to_sym end end OPERATOR = { :[] => :element_read, :[]= => :element_write, :** => :power, :~ => :not, :+@ => :unary_plus, :-@ => :unary_minus, :* => :product, :/ => :quotient, :% => :modulo, :+ => :plus, :- => :minus, :>> => :right_shift, :<< => :left_shift, :& => :and, :^ => :xor, :| => :or, :<= => :less_or_equal, :< => :less, :> => :greater, :>= => :greater_or_equal, :<=> => :comparison, :== => :eql, :=== => :case_comparison, :=~ => :match } end