Class: NestedArray

Inherits:
Array show all
Extended by:
Assertions::ClassMethods, ClassMethods
Includes:
Assertions, Constants
Defined in:
app/models/nested_array.rb

Overview

Copyright (C) 2010-2013 by Greg Lawson

<[email protected]>

Copyright: See COPYING file that comes with this distribution

parse tree internal format is nested Arrays. Postfix operators and brackets start embeddded arrays

Direct Known Subclasses

RegexpTree

Defined Under Namespace

Modules: Assertions, ClassMethods, Constants, Examples

Instance Method Summary collapse

Methods included from Assertions::ClassMethods

assert_post_conditions

Methods included from Assertions

#assert_post_conditions, #assert_pre_conditions

Constructor Details

#initialize(array = []) ⇒ NestedArray

Returns a new instance of NestedArray.


17
18
19
# File 'app/models/nested_array.rb', line 17

def initialize(array=[])
	super(array)
end

Instance Method Details

#[](index) ⇒ Object

Makes sure all descendant classes return the proper nested type. Calls super [] and if an Array is returned promotes it to self's class If returned object is not an Array (e.g. leaf node) retun it unchanged.


27
28
29
30
31
32
33
# File 'app/models/nested_array.rb', line 27

def [](index)
	if super(index).kind_of?(Array) then
		return promote(super(index))
	else # unnested node
		return at(index)
	end #if
end

#map_branches(&visit_proc) ⇒ Object

Apply block to each non-leaf or branching node Provides a postfix walk Two passes: 1) Recursively visit descendants 2) Visit branching nodes (Arrays) Desirable since result tree is constructed bottom-up Descendants have the block applied before they are reassembled into a tree. Branching node block can take into account changes in subtrees.


64
65
66
67
68
69
70
71
72
73
# File 'app/models/nested_array.rb', line 64

def map_branches(&visit_proc)
	visited_subtrees= self.map do |sub_tree| 
		if sub_tree.kind_of?(Array) then
			self.class.new(sub_tree).map_branches{|p| visit_proc.call(p)}
		else
			sub_tree
		end #if
	end
	return visit_proc.call(visited_subtrees, &visit_proc)
end

#map_recursive(&visit_proc) ⇒ Object

Apply block to each leaf. Nesting structure remains the same. Array#map will only process the top level Array.


46
47
48
49
50
51
52
53
54
# File 'app/models/nested_array.rb', line 46

def map_recursive(&visit_proc)
	return self.map do |sub_tree| 
		if sub_tree.kind_of?(Array) then
			NestedArray.new(sub_tree).map_recursive{|p| visit_proc.call(p)}
		else
			visit_proc.call(sub_tree) # end recursion
		end #if
	end
end

#merge_single_element_arrays?Boolean

Probably less confusing as ! The method makes no sense as a question

Returns:

  • (Boolean)

76
77
78
79
80
81
82
83
84
85
# File 'app/models/nested_array.rb', line 76

def merge_single_element_arrays?
	map_branches do |branch| # visit all in postfix order
		if branch.size==1 && branch[0].kind_of?(Array) then
			self.class.new(branch[0]) # remove redundant brankets
		else
			self.class.new(branch)
		end #if
	end #map_branches

end

#promote(value) ⇒ Object

new object, but of type of self


21
22
23
# File 'app/models/nested_array.rb', line 21

def promote(value)
	return self.class.new(value)
end

#reverseObject

reverse nested array


35
36
37
# File 'app/models/nested_array.rb', line 35

def reverse
	promote(super)
end

#to_sObject

Should apply to_s to each element before returning Use map_recursive


40
41
42
# File 'app/models/nested_array.rb', line 40

def to_s
	return map_recursive{|leaf| leaf.to_s}.flatten.join
end