Class: Array

Inherits:
Object show all
Defined in:
lib/relisp/type_conversion/programming_types.rb

Overview

The normal Array class is modified so that an array can be converted to either Relisp::List or Relisp::Vector.

Direct Known Subclasses

Relisp::List, Relisp::Vector

Constant Summary collapse

@@default_elisp_type =
Relisp::List

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_elisp_typeObject



449
450
451
# File 'lib/relisp/type_conversion/programming_types.rb', line 449

def self.default_elisp_type
  @@default_elisp_type
end

.default_elisp_type=(type) ⇒ Object

Set the elisp type that ruby arrays are converted to by default.



441
442
443
444
445
446
447
# File 'lib/relisp/type_conversion/programming_types.rb', line 441

def self.default_elisp_type=(type)
  unless type.ancestors.include?(Array)
    raise ArgumentError, "#{type} is not a kind of Array" 
  end

  @@default_elisp_type = type
end

.from_elisp(object) ⇒ Object

Converts either a ‘cons’ or ‘vector’ to a ruby array.



428
429
430
431
432
433
434
435
436
437
438
# File 'lib/relisp/type_conversion/programming_types.rb', line 428

def self.from_elisp(object)
  object_variable = object[:slave].get_permanent_variable(object[:variable])
  size = object[:slave].elisp_eval( "(length #{object_variable})" )
  object_array = new
  size.times do |i|
    object_array << object[:slave].elisp_eval( "(elt #{object_variable} #{i.to_elisp})" )
  end

  object[:slave].elisp_exec( "(makunbound #{object_variable.to_elisp})" )
  return object_array
end

Instance Method Details

#elisp_typeObject

The elisp type that this array will be converted to by to_elisp.



454
455
456
457
# File 'lib/relisp/type_conversion/programming_types.rb', line 454

def elisp_type
  @elisp_type = nil unless defined?(@elisp_type) #to avoid uninitialized warning
  @elisp_type || @@default_elisp_type
end

#elisp_type=(type) ⇒ Object



459
460
461
462
463
464
465
# File 'lib/relisp/type_conversion/programming_types.rb', line 459

def elisp_type=(type)
  unless type.ancestors.include?(Array)
    raise ArgumentError, "#{type} is not a kind of Array" 
  end

  @elisp_type = type
end

#to_elispObject



467
468
469
# File 'lib/relisp/type_conversion/programming_types.rb', line 467

def to_elisp
  elisp_type.new(self).to_elisp
end