Class: Prune::PObjects::PArray

Inherits:
Base
  • Object
show all
Defined in:
lib/prune/p_objects/p_array.rb

Constant Summary collapse

LF_SIZE =

Size to line feed when converted to String.

10

Instance Method Summary collapse

Methods inherited from Base

#indent!, #outdent!, #space, #value_to_s

Constructor Details

#initialize(*args) ⇒ PArray

Initialize.



10
11
12
# File 'lib/prune/p_objects/p_array.rb', line 10

def initialize(*args)
  @array = args
end

Instance Method Details

#<<(value) ⇒ Object



18
19
20
# File 'lib/prune/p_objects/p_array.rb', line 18

def <<(value)
  @array << value
end

#[](key) ⇒ Object



22
23
24
# File 'lib/prune/p_objects/p_array.rb', line 22

def [](key)
  @array[key]
end

#[]=(key, value) ⇒ Object



26
27
28
# File 'lib/prune/p_objects/p_array.rb', line 26

def []=(key, value)
  @array[key] = value
end

#empty?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/prune/p_objects/p_array.rb', line 14

def empty?
  @array.empty?
end

#sizeObject



30
31
32
# File 'lib/prune/p_objects/p_array.rb', line 30

def size
  @array.size
end

#to_sObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/prune/p_objects/p_array.rb', line 34

def to_s
  unless self.empty?
    # 配列が空でない場合
    if @array.size > LF_SIZE
      # 配列長がLF_SIZEを越える場合
      head = @array[0, LF_SIZE]
      tail = @array[LF_SIZE, @array.size]
      out = []
      out << "[ %s" % head.collect{|item| value_to_s(item)}.join(" ")
      indent!
      tail.each_slice(LF_SIZE){|slice|
        out << space + slice.collect{|item| value_to_s(item)}.join(" ")
      }
      outdent!
      out[out.size - 1] += " ]"
      out.join(LF)
    else
      # 配列長がLF_SIZE以下の場合
      "[ %s ]" % @array.collect{|item| value_to_s(item)}.join(" ")
    end
  else
    # 配列が空の場合
    "[ ]"
  end
end