Class: ANSI::Code
Overview
The object that calling ANSI.define(…) actually creates. This is managed internally and you should rarely have to interface with it directly.
Instance Attribute Summary collapse
-
#arity ⇒ Object
readonly
Returns the value of attribute arity.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
Instance Method Summary collapse
-
#===(other) ⇒ Object
Returns true if the other object is a kind_of ANSI::Code, or if the other object is a kind_of ANSI::Match and this ANSI::Code is contained in that match.
-
#add_methods!(*names) ⇒ Object
Creates methods with the specified names which are aliases of #generate.
-
#initialize(*names, &block) ⇒ Code
constructor
Takes an optional list of method aliases, which are sent to #add_methods!, and a mandatory block argument which is bound to the #generate method.
-
#name ⇒ Object
(also: #to_s, #inspect)
The name of this code.
Methods included from ANSI
alias_code, arities, codes, define_with_extension, dynamically_defined_methods, generate_sequence, recognize, reset!, test_sequence
Constructor Details
#initialize(*names, &block) ⇒ Code
Takes an optional list of method aliases, which are sent to #add_methods!, and a mandatory block argument which is bound to the #generate method.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ansi/code.rb', line 57 def initialize(*names, &block) @arity = block.arity || 0 @block = block eigen.instance_eval do if block.arity > 0 # we need a list of arguments to pass to it. define_method "generate" do |*args| # this is to get around the warnings in Ruby 1.8 if args.length > block.arity && block.arity == 1 raise ArgumentError, "too many arguments (#{args.length} for #{block.arity})", caller end # omitted args should be made nil. We do this explicitly to silence warnings in 1.8. if (len = block.arity - args.length) len.times { args << nil } end instance_exec(*args, &block) end else define_method("generate", &block) end end add_methods! *names end |
Instance Attribute Details
#arity ⇒ Object (readonly)
Returns the value of attribute arity.
5 6 7 |
# File 'lib/ansi/code.rb', line 5 def arity @arity end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
5 6 7 |
# File 'lib/ansi/code.rb', line 5 def block @block end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
5 6 7 |
# File 'lib/ansi/code.rb', line 5 def method_name @method_name end |
Instance Method Details
#===(other) ⇒ Object
Returns true if the other object is a kind_of ANSI::Code, or if the other object is a kind_of ANSI::Match and this ANSI::Code is contained in that match. In other words:
code = ANSI::Code.new("a_code") {}
match = ANSI::Match.new
code === match
#=> false
match << code
code === match
#=> true
29 30 31 32 33 34 35 36 37 |
# File 'lib/ansi/code.rb', line 29 def ===(other) if other.kind_of?(ANSI::Code) return true elsif other.kind_of?(ANSI::Match) && other == self return true else return false end end |
#add_methods!(*names) ⇒ Object
Creates methods with the specified names which are aliases of #generate. These methods are singletons of this instance of ANSI::Code, so adding method names using this method is only useful within the context of this instance.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ansi/code.rb', line 43 def add_methods!(*names) #:nodoc: names = names.flatten return if names.empty? @method_name ||= names.first.to_s names.each do |name| eigen.instance_eval do alias_method name, :generate end end end |
#name ⇒ Object Also known as: to_s, inspect
The name of this code. This is assigned to the first non-nil method name that this object uses.
9 10 11 |
# File 'lib/ansi/code.rb', line 9 def name @name || (@method_name.kind_of?(String) ? @name = @method_name : @method_name.to_s).upcase end |