Class: Lab42::Console::Kwd

Inherits:
Object show all
Extended by:
Tools::ForwardToNewInstance
Defined in:
lib/lab42/console/kwd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tools::ForwardToNewInstance

method_missing

Instance Attribute Details

#kwdsObject (readonly)

Returns the value of attribute kwds.



8
9
10
# File 'lib/lab42/console/kwd.rb', line 8

def kwds
  @kwds
end

Instance Method Details

#count(*keys) ⇒ Object



11
12
13
# File 'lib/lab42/console/kwd.rb', line 11

def count *keys
  kwds.count{ |pairs| keys.include? pairs.keys.first }
end

#first_value(*keys) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
# File 'lib/lab42/console/kwd.rb', line 16

def first_value *keys
  raise ArgumentError, "need 0, 1, got #{keys.size}" unless keys.size < 2
  key = keys.first
  if key
    kwds.find{ |pairs| pairs.keys.first == key }.values.first
  else
    kwds.first.values.first
  end
end

#last_value(*keys) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
# File 'lib/lab42/console/kwd.rb', line 26

def last_value *keys
  raise ArgumentError, "need 0, 1, got #{keys.size}" unless keys.size < 2
  key = keys.first
  if key
    kwds.reverse.find{ |pairs| pairs.keys.first == key }.values.first
  else
    kwds.reverse.first.values.first
  end
end

#values(*keys) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/lab42/console/kwd.rb', line 36

def values *keys
  if keys.empty?
    kwds.map{ |pairs| pairs.values.first }
  else
    kwds.map{ |pairs| keys.include?(pairs.keys.first) && pairs.values.first }.select(&:identity)
  end
end