Moving Elements in a Ruby Array by Name

warning: Invalid argument supplied for foreach() in /home/snuxoll/snuxoll.com/modules/jaikublock/jaiku.module on line 259.

Tagged:  

    This is an extension to my previous tip about moving elements in ruby arrays around. In fact, this is yet another mixin that only has one line of real code, and even though it's quite obvious I thought I'd post it here.

# Adds the ability to reoder elements by name,
# it requires the object extended
# to support the Reorderable module and have
# a #index_of method
module NameReorderable
  # Moves an element around an orderable
  # list by its name
  def move_by_name(name, to)
    move(index(name), to)
  end
end

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

Content Copyright ©2008 Stefan Nuxoll