Class: Envo::ListVal
- Inherits:
-
Object
- Object
- Envo::ListVal
- Defined in:
- lib/envo/val/list_val.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#ar ⇒ Object
readonly
Returns the value of attribute ar.
Instance Method Summary collapse
- #accept_assign?(other) ⇒ Boolean
- #accept_item?(item) ⇒ Boolean
- #clean! ⇒ Object
- #delete(elem) ⇒ Object
- #delete_at(index) ⇒ Object
-
#initialize(ar) ⇒ ListVal
constructor
A new instance of ListVal.
- #insert(elem, pos = nil) ⇒ Object
- #invalid_description ⇒ Object
- #list? ⇒ Boolean
- #pp_attribs(elem) ⇒ Object
- #pretty_print(ctx) ⇒ Object
- #shift(elem, dir) ⇒ Object
- #shift_at(i, dir) ⇒ Object
- #to_list ⇒ Object
- #to_s ⇒ Object
-
#type ⇒ Object
casts.
- #uniq! ⇒ Object
Constructor Details
#initialize(ar) ⇒ ListVal
Returns a new instance of ListVal.
3 4 5 |
# File 'lib/envo/val/list_val.rb', line 3 def initialize(ar) @ar = ar end |
Instance Attribute Details
#ar ⇒ Object (readonly)
Returns the value of attribute ar.
6 7 8 |
# File 'lib/envo/val/list_val.rb', line 6 def ar @ar end |
Instance Method Details
#accept_assign?(other) ⇒ Boolean
88 89 90 |
# File 'lib/envo/val/list_val.rb', line 88 def accept_assign?(other) other.list? end |
#accept_item?(item) ⇒ Boolean
100 101 102 |
# File 'lib/envo/val/list_val.rb', line 100 def accept_item?(item) true end |
#clean! ⇒ Object
34 35 36 |
# File 'lib/envo/val/list_val.rb', line 34 def clean! uniq! end |
#delete(elem) ⇒ Object
25 26 27 |
# File 'lib/envo/val/list_val.rb', line 25 def delete(elem) @ar.delete(elem) end |
#delete_at(index) ⇒ Object
28 29 30 |
# File 'lib/envo/val/list_val.rb', line 28 def delete_at(index) @ar.delete_at(index) end |
#insert(elem, pos = nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/envo/val/list_val.rb', line 8 def insert(elem, pos = nil) # assume unique elements old_index = @ar.index(elem) new_index = case pos when :front then 0 when :back then -1 else old_index end return @ar << elem if !new_index return @ar if new_index == old_index return @ar.insert(new_index, elem) if !old_index # we need to reorder @ar.delete_at(old_index) @ar.insert(new_index, elem) end |
#invalid_description ⇒ Object
91 92 93 |
# File 'lib/envo/val/list_val.rb', line 91 def invalid_description @ar.empty? ? "empty list" : nil end |
#list? ⇒ Boolean
94 95 96 |
# File 'lib/envo/val/list_val.rb', line 94 def list? true end |
#pp_attribs(elem) ⇒ Object
70 71 72 |
# File 'lib/envo/val/list_val.rb', line 70 def pp_attribs(elem) @ar.count(elem) > 1 ? 'D' : ' ' end |
#pretty_print(ctx) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/envo/val/list_val.rb', line 73 def pretty_print(ctx) ctx.puts "[" @ar.each_with_index do |v, i| str = pp_attribs(v) + ' ' str += "#{i}:".ljust(4) str += v ctx.puts str end ctx.puts ']' end |
#shift(elem, dir) ⇒ Object
37 38 39 40 41 |
# File 'lib/envo/val/list_val.rb', line 37 def shift(elem, dir) i = @ar.index(elem) return nil if i == nil shift_at(i, dir) end |
#shift_at(i, dir) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/envo/val/list_val.rb', line 42 def shift_at(i, dir) return nil if i>@ar.size if dir == :front return i if i == 0 elem = ar[i] @ar.delete_at i @ar.unshift(elem) 0 elsif dir == :back return i if i == (@ar.size-1) elem = ar[i] @ar.delete_at i @ar << elem @ar.size-1 elsif dir == :up return i if i == 0 @ar[i-1], @ar[i] = @ar[i], @ar[i-1] i - 1 elsif dir == :down return i if i == (@ar.size-1) @ar[i+1], @ar[i] = @ar[i], @ar[i+1] i + 1 else -1 end end |
#to_list ⇒ Object
97 98 99 |
# File 'lib/envo/val/list_val.rb', line 97 def to_list return self end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/envo/val/list_val.rb', line 103 def to_s raise StandardError.new "list can't be converted to String" end |
#type ⇒ Object
casts
85 86 87 |
# File 'lib/envo/val/list_val.rb', line 85 def type :list end |
#uniq! ⇒ Object
31 32 33 |
# File 'lib/envo/val/list_val.rb', line 31 def uniq! @ar.uniq! end |