Class: Psych::Pure::LoadedArray
- Inherits:
-
LoadedObject
- Object
- SimpleDelegator
- LoadedObject
- Psych::Pure::LoadedArray
- Defined in:
- lib/psych/pure.rb
Overview
Wraps a Ruby array with its node from the source input.
Every Array method falls into one of these buckets:
-
Additive (overridden, no dirty) — <<, push/append, unshift/prepend, insert, concat, fill. The relative gaps between existing elements' line numbers are still valid, so blank line preservation still works.
-
Replacement (overridden, conditional dirty) — []=. Only sets dirty when the array length changes (range-based deletion/insertion), not on simple element replacement.
-
Reordering (overridden, always dirty) — sort!, sort_by!, shuffle!, reverse!, rotate!. Line numbers become meaningless after reordering.
-
C-level fix (overridden) — compact, compact!. Array#compact uses a C-level nil check (NIL_P) that doesn't see through the delegator.
-
Removal (method_missing, dirty via dup+eql?) — delete, delete_at, pop, shift, reject!, select!, slice!, clear, replace, uniq!, flatten!, delete_if, keep_if. These fall through to LoadedObject's method_missing which sets dirty when the array actually changes.
-
Non-bang variants (return plain Array) — select, reject, map, sort, reverse, uniq, flatten, compact, +, -, &, |, etc. These return a new Array without array-level metadata, but the elements are shared references that still carry their comments.
-
Read-only (delegation, no mutation) — each, [], include?, length, first, last, etc. SimpleDelegator handles these automatically.
Instance Attribute Summary
Attributes inherited from LoadedObject
Instance Method Summary collapse
-
#<<(element) ⇒ Object
Additive — no dirty.
-
#[]=(index, *args) ⇒ Object
Replacement — conditional dirty.
-
#compact ⇒ Object
C-level fix — Array#compact uses NIL_P which doesn't see through the delegator, so we implement nil detection manually.
- #compact! ⇒ Object
- #concat(*arrays) ⇒ Object
- #fill(*args, &block) ⇒ Object
- #insert(index, *elements) ⇒ Object
- #push(*elements) ⇒ Object (also: #append)
- #reverse! ⇒ Object
- #rotate!(count = 1) ⇒ Object
- #shuffle!(**kwargs) ⇒ Object
-
#sort!(&block) ⇒ Object
Reordering — always dirty.
- #sort_by!(&block) ⇒ Object
- #unshift(*elements) ⇒ Object (also: #prepend)
Methods inherited from LoadedObject
#initialize, #initialize_clone, #initialize_dup
Constructor Details
This class inherits a constructor from Psych::Pure::LoadedObject
Instance Method Details
#<<(element) ⇒ Object
Additive — no dirty
292 293 294 295 |
# File 'lib/psych/pure.rb', line 292 def <<(element) __getobj__ << element self end |
#[]=(index, *args) ⇒ Object
Replacement — conditional dirty
328 329 330 331 332 333 334 |
# File 'lib/psych/pure.rb', line 328 def []=(index, *args) target = __getobj__ previous = target.length result = target.[]=(index, *args) @dirty = true if target.length != previous result end |
#compact ⇒ Object
C-level fix — Array#compact uses NIL_P which doesn't see through the delegator, so we implement nil detection manually.
371 372 373 |
# File 'lib/psych/pure.rb', line 371 def compact __getobj__.reject { |element| nil_element?(element) } end |
#compact! ⇒ Object
375 376 377 378 379 380 381 382 |
# File 'lib/psych/pure.rb', line 375 def compact! target = __getobj__ previous = target.length target.reject! { |element| nil_element?(element) } changed = target.length != previous @dirty = true if changed changed ? self : nil end |
#concat(*arrays) ⇒ Object
316 317 318 319 |
# File 'lib/psych/pure.rb', line 316 def concat(*arrays) __getobj__.concat(*arrays) self end |
#fill(*args, &block) ⇒ Object
321 322 323 324 |
# File 'lib/psych/pure.rb', line 321 def fill(*args, &block) __getobj__.fill(*args, &block) self end |
#insert(index, *elements) ⇒ Object
311 312 313 314 |
# File 'lib/psych/pure.rb', line 311 def insert(index, *elements) __getobj__.insert(index, *elements) self end |
#push(*elements) ⇒ Object Also known as: append
297 298 299 300 |
# File 'lib/psych/pure.rb', line 297 def push(*elements) __getobj__.push(*elements) self end |
#reverse! ⇒ Object
356 357 358 359 360 |
# File 'lib/psych/pure.rb', line 356 def reverse! __getobj__.reverse! @dirty = true self end |
#rotate!(count = 1) ⇒ Object
362 363 364 365 366 |
# File 'lib/psych/pure.rb', line 362 def rotate!(count = 1) __getobj__.rotate!(count) @dirty = true self end |
#shuffle!(**kwargs) ⇒ Object
350 351 352 353 354 |
# File 'lib/psych/pure.rb', line 350 def shuffle!(**kwargs) __getobj__.shuffle!(**kwargs) @dirty = true self end |
#sort!(&block) ⇒ Object
Reordering — always dirty
338 339 340 341 342 |
# File 'lib/psych/pure.rb', line 338 def sort!(&block) __getobj__.sort!(&block) @dirty = true self end |
#sort_by!(&block) ⇒ Object
344 345 346 347 348 |
# File 'lib/psych/pure.rb', line 344 def sort_by!(&block) __getobj__.sort_by!(&block) @dirty = true self end |
#unshift(*elements) ⇒ Object Also known as: prepend
304 305 306 307 |
# File 'lib/psych/pure.rb', line 304 def unshift(*elements) __getobj__.unshift(*elements) self end |