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



403
404
405
# File 'lib/relisp/type_conversion/programming_types.rb', line 403

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.



395
396
397
398
399
400
401
# File 'lib/relisp/type_conversion/programming_types.rb', line 395

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.



382
383
384
385
386
387
388
389
390
391
392
# File 'lib/relisp/type_conversion/programming_types.rb', line 382

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.



408
409
410
411
# File 'lib/relisp/type_conversion/programming_types.rb', line 408

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

#elisp_type=(type) ⇒ Object



413
414
415
416
417
418
419
# File 'lib/relisp/type_conversion/programming_types.rb', line 413

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



421
422
423
# File 'lib/relisp/type_conversion/programming_types.rb', line 421

def to_elisp
  elisp_type.new(self).to_elisp
end