Moving Elements in a Ruby Array by Name

    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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

supplynflshop

thank you for sharing
http://www.supplynflshop.com

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