Module: Pyper

Included in:
Array, Enumerable, Hash, Range, String
Defined in:
lib/pyper.rb,
lib/pyper/version.rb

Overview

Pyper is an extension of the Lispy car/cdr idea.

Defined Under Namespace

Modules: ControlCharacters Classes: PostfixMachine

Constant Summary collapse

VERSION =
"2.0.0"
DEBUG =

debug level

0

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

Reacts to the method symbols delimited with Pyper control characters (τ, π, and χ), compiles them into appropriate methods, and calls them.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/pyper.rb', line 150

def method_missing( symbol, *args, &block )
  str = symbol.to_s
  super if str.starts_with? 'to_' # quick exclude of "to_something" methods
  puts "received msg #{str}" if Pyper::DEBUG > 1
  case str
  when /^τ(.+)τ$/ then pyper_mm( symbol, $1, op: 1, ret: 1 )
  when /^π(.+)τ$/ then pyper_mm( symbol, $1, op: 2, ret: 1 )
  when /^χ(.+)τ$/ then pyper_mm( symbol, $1, op: -2, ret: 1 )
  when /^τ(.+)π$/ then pyper_mm( symbol, $1, op: 1, ret: 2 )
  when /^π(.+)π$/ then pyper_mm( symbol, $1, op: 2, ret: 2 )
  when /^χ(.+)π$/ then pyper_mm( symbol, $1, op: -2, ret: 2 )
  when /^τ(.+)χ$/ then pyper_mm( symbol, $1, op: 1, ret: -2 )
  when /^π(.+)χ$/ then pyper_mm( symbol, $1, op: 2, ret: -2 )
  when /^χ(.+)χ$/ then pyper_mm( symbol, $1, op: -2, ret: -2 )
  else super end
  send symbol, *args, &block # call it right away
end

Instance Method Details

#caarObject

In their basic form, they are only marginally useful. Their popularity stems from their compositions:



17
# File 'lib/pyper.rb', line 17

def caar; first.first end

#cadrObject



19
# File 'lib/pyper.rb', line 19

def cadr; drop(1).first end

#carObject

Everybody knows Lispy functions #car, #cdr. In Ruby, these functions can be defined for example as:



12
# File 'lib/pyper.rb', line 12

def car; first end

#cdarObject



18
# File 'lib/pyper.rb', line 18

def cdar; first.drop 1 end

#cddrObject



20
# File 'lib/pyper.rb', line 20

def cddr; drop(1).drop(1) end

#cdrObject

d: all except first



13
# File 'lib/pyper.rb', line 13

def cdr; drop 1 end

#respond_to_missing?(symbol, include_private = false) ⇒ Boolean

Respond-to method matching to Pyper#method_missing.

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
178
# File 'lib/pyper.rb', line 170

def respond_to_missing?( symbol, include_private = false )
  str = symbol.to_s
  super if str.starts_with? 'to_'
  case str
  when /^τ(\w+)τ$/, /^π(\w+)τ$/, /^χ(\w+)τ$/,
       /^τ(\w+)π$/, /^π(\w+)π$/, /^χ(\w+)π$/,
       /^τ(\w+)χ$/, /^π(\w+)χ$/, /^χ(\w+)χ$/ then true
  else super end
end