# ============================================================================== # EXTENDING CLASS Hash # EXTENDING CLASS String # ============================================================================== # # (C) Copyright 2007 by Tilo Sloboda # # updated: Time-stamp: # # License: # Freely available under the terms of the OpenSource "Artistic License" # in combination with the Addendum A (below) # # In case you did not get a copy of the license along with the software, # it is also available at: http://www.unixgods.org/~tilo/artistic-license.html # # Addendum A: # THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU! # SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, # REPAIR OR CORRECTION. # # IN NO EVENT WILL THE COPYRIGHT HOLDERS BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, # SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY # TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED # INACCURATE OR USELESS OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM # TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF THE COPYRIGHT HOLDERS OR OTHER PARTY HAS BEEN # ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. # ============================================================================== # # Homepage: http://www.unixgods.org/~tilo/Ruby/hash_add_pairs.rb # # ============================================================================== # # This file provides an extension to class String: # # String.to_array(input,*separator) # splits up a string into an array. # automatically splits on commas or spaces # # and an extension to class Hash: # # Hash.add_pairs(keys,values,*separator) # adds key/value pairs from given keys and values # inputs can be either Strings or Arrays # # ============================================================================== # EXTENDING CLASS String # ============================================================================== class String def to_array(input,*separator) # splits a given string and returns an array # # if no separator is provided, this method first # checks if the string is a comma separated list, then # it checks if the string is a space-separated list # otherwise returns input un-altered # if input.class == String if separator.size > 0 # split with provided separator a = input.split(separator.first) return a elsif input =~ /,/ # comma separated values a = input.split(/,/) return a elsif input =~ /\s/ # space separated values a = input.split(/\s/) return a else return input # otherwise return what we got end else # probably want to raise an exception here.. return input # otherwise return what we got end end end # ============================================================================== # EXTENDING CLASS Hash # ============================================================================== # uses String.to_array() class Hash def add_pairs(keys,values,*separator) # the keys and values can be either given as a String or an Array # the separator is ignored if the inputs are Arrays # if the inputs are Strings, both have to be either comma or space separated, # or a separator has to be given (which is used for both keys and values) # # # ATTENTION: # we need to be sure to not modify or destroy the original inputs, because # this could break things in the outside world.. # That's why we clone the inputs here: # if separator.size > 0 keys = (keys.class == String ? String.to_array(keys,separator.first) : keys.clone) values = (values.class == String ? String.to_array(values,separator.first) : values.clone) else keys = (keys.class == String ? String.to_array(keys) : keys.clone) values = (values.class == String ? String.to_array(values) : values.clone) end if keys.class == Array and values.class == Array # NOTE: keys, values are either new objects, or clones of the original inputs, # and can be destroyed. while k = keys.shift and v = values.shift self[k] = v end return self else # probably want to raise an exception here.. puts "keys is #{keys.class} , values is #{values.class}" end return self end end