Module: NameMagic::ArrayMethods
- Defined in:
- lib/y_support/name_magic/array_methods.rb
Instance Method Summary collapse
-
#_names_(option = nil) ⇒ Object
(also: #ɴ)
Maps an array to an array of the element names, obtained by applying
#_name_method to them. -
#names(option = nil) ⇒ Object
(also: #full_names)
Maps an array of some objects into an array of their names, obtained by applying
#full_namemethod to them.
Instance Method Details
#_names_(option = nil) ⇒ Object Also known as: ɴ
Maps an array to an array of the element names, obtained by
applying #_name_ method to them. Takes one optional argument,
which regulates its behavior regarding unnamed objects. If set
to nil (default) unnamed objects will be mapped to nil
(default behavior of #_name_ method). If set to true,
unnamed objects will be mapped to themselves. If set to
false, unnamed objects will not be mapped at all -- the
returned array will contain only the names of the named
objects.
38 39 40 41 42 43 44 45 46 |
# File 'lib/y_support/name_magic/array_methods.rb', line 38 def _names_ option=nil # unnamed --> nil return map &:_name_ if option.nil? # unnamed --> instance return map { |e| e.name || e } if option == true # unnamed squeezed out return map( &:_name_ ).compact if option == false fail ArgumentError, "Unknown option: #{option}" end |
#names(option = nil) ⇒ Object Also known as: full_names
Maps an array of some objects into an array of their names,
obtained by applying #full_name method to them. Takes one
optional argument, which regulates its behavior regarding
unnamed objects. If set to nil (default), unnamed objects
will be mapped to nil (default behavior of the #name
method). If set to true, unnamed objects will be mapped to
themselves. If set to false, unnamed objects will not be
mapped at all -- the returned array will contain only the names
of the named objects.
14 15 16 17 18 19 20 21 22 |
# File 'lib/y_support/name_magic/array_methods.rb', line 14 def names option=nil # unnamed --> nil return map &:name if option.nil? # unnamed --> instance return map { |e| e.name || e } if option == true # unnamed squeezed out return map( &:name ).compact if option == false fail ArgumentError, "Unknown option: #{option}" end |