Class: Relisp::Cons

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

Overview

A proxy to an Emacs cons. If the cons is actually a list, the to_list method will convert it a subclass of Array so that all of the ruby Array stuff is available.

Instance Attribute Summary

Attributes inherited from Proxy

#elisp_variable, #slave

Instance Method Summary collapse

Methods inherited from Proxy

from_elisp, #to_elisp

Constructor Details

#initialize(*args) ⇒ Cons

args can be any of these forms:

  • (symbol, slave = Relisp.default_slave)

  • (_car, cdr, slave = Relisp.default_slave)

When a symbol is given it is considered to be the name of a pre-existing cons in the slave process. Otherwise a new cons is created with the given car and _cdr (cons).



194
195
196
197
198
# File 'lib/relisp/type_conversion/programming_types.rb', line 194

def initialize(*args)
  super do |car, cdr|
    @slave.elisp_exec( "(setq #{@elisp_variable} (cons #{car.to_elisp} #{cdr.to_elisp}))")
  end
end

Instance Method Details

#caarObject

Return the car of the car of the Cons. (caar)



226
227
228
# File 'lib/relisp/type_conversion/programming_types.rb', line 226

def caar
  @slave.caar(@elisp_variable.value)
end

#cadrObject

Return the car of the cdr of the Cons. (cadr)



232
233
234
# File 'lib/relisp/type_conversion/programming_types.rb', line 232

def cadr
  @slave.cadr(@elisp_variable.value)
end

#carObject

Return the car of the Cons. (car).



214
215
216
# File 'lib/relisp/type_conversion/programming_types.rb', line 214

def car
  @slave.car(@elisp_variable.value)
end

#car=(newcar) ⇒ Object

Set the car of Cons to be newcar (setcar).



202
203
204
# File 'lib/relisp/type_conversion/programming_types.rb', line 202

def car=(newcar)
  @slave.setcar(@elisp_variable.value, newcar)
end

#cdarObject

Return the cdr of the car of the Cons. (cdar)



238
239
240
# File 'lib/relisp/type_conversion/programming_types.rb', line 238

def cdar
  @slave.cadr(@elisp_variable.value)
end

#cddrObject

Return the cdr of the cdr of the Cons. (cddr)



244
245
246
# File 'lib/relisp/type_conversion/programming_types.rb', line 244

def cddr
  @slave.cadr(@elisp_variable.value)
end

#cdrObject

Return the cdr of the Cons. (cdr).



220
221
222
# File 'lib/relisp/type_conversion/programming_types.rb', line 220

def cdr
  @slave.cdr(@elisp_variable.value)
end

#cdr=(newcdr) ⇒ Object

Set the cdr of Cons to be newcdr. (setcdr).



208
209
210
# File 'lib/relisp/type_conversion/programming_types.rb', line 208

def cdr=(newcdr)
  @slave.setcdr(@elisp_variable.value, newcdr)
end

#list?Boolean

This function will NOT return true whenever the elisp function listp is true. The elisp function is true whenever the object is a cons cell, but this method is true only when the cons can be unambiguously translated to an array; this condition is satisfied when the object in question could have been written using the elisp function list.

Returns:

  • (Boolean)


255
256
257
258
259
260
261
262
263
264
265
# File 'lib/relisp/type_conversion/programming_types.rb', line 255

def list?
  current_cdr = cdr
  while current_cdr.kind_of?(Cons)
    current_cdr = current_cdr.cdr
  end
  if current_cdr.nil?
    return true
  else
    return false
  end
end

#to_listObject Also known as: to_a

Translate the cons cell into a Relisp::List, a subclass of Array. See list? for when this be done.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/relisp/type_conversion/programming_types.rb', line 270

def to_list
  list_array = []
  list_array << car
  current_cdr = cdr
  while current_cdr.kind_of?(Cons)
    list_array << current_cdr.car
    current_cdr = current_cdr.cdr
  end

  if current_cdr.nil?
    return Relisp::List.new(list_array)
  else
    return false
  end
end