Ruby - Tidbits

Some small awesome features of Ruby scripting language (especially for InfoNet and InfoWorks)

Use map with row_object_collections!

Generally row object collections are a lot faster to deal with as they only hold a reference to a value and not the data itself. However sometimes they can be frustrating to use, because they don't have common Ruby functions like map,select etc. For some methods we have built in methods to deal with this. For example:


      WSOpenNetwork#row_object_collection   #collection
      WSOpenNetwork#row_objects             #array
      
      WSNodeObject.us_links                 #collection
      WSNodeObject.navigate("us_links")     #array
    

But in some examples there is no alternative. E.G.


      WSOpenNetwork#row_object_collection_selection   #collection
    

Recently I discovered a way of obtaining these methods on any RowObjectCollection object! Here's how! We call the enum_for method on the parent object and parse in the symbol for the each method:


      new = WSApplication.current_network
      
      net.row_object_collection_selection("cams_cctv_survey")                   #Collection
      net.row_object_collection_selection("cams_cctv_survey").enum_for(:each)   #Enumerator
    

Enumerator gives us access to many more methods:


      inspect
      size
      each
      each_with_index
      each_with_object
      next
      rewind
      find
      entries
      select
      sort
      sort_by
      reject
      grep
      collect
      map
      reduce
      group_by
      ... and more! ...
    

Next tidbit