SHARE
TWEET

Untitled

a guest Jul 21st, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def combine_first_and_last(key_value_pairs)
  2.  
  3.     keys = key_value_pairs.keys
  4.     values  = key_value_pairs.values
  5.     new_key = keys.first.to_s << keys.last.to_s
  6.     if values[0].class == Array
  7.       new_value = key_value_pairs.values.first + key_value_pairs.values.last
  8.     else
  9.       new_value = key_value_pairs.values.first.to_s + key_value_pairs.values.last.to_s
  10.     end
  11.     key_value_pairs.delete(keys.first)
  12.     key_value_pairs.delete(keys.last)
  13.     new_keys = {new_key => new_value}.merge(key_value_pairs)
  14. end
  15. puts combine_first_and_last({ a: [1], c: 5, x: [2] })
  16. p combine_first_and_last({ a: 's', c: 5, x: 'g' })
  17. puts combine_first_and_last({ a: 2, c: 5, x: 3 })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top