It provides some extensions to the ruby core.
Installation
Add gem "vidibus-core_extensions"
to your Gemfile. Then call bundle install
on your console.
Usage
Array
Array#flatten_once
Flattens first level of an array. Example:
[1, [2, [3]]].flatten_once # => [1, 2, [3]]
Array#merge
Merges given array with current one.
It will perform insertion of new items by three rules:
- If the item's predecessor is present, insert item after it.
- If the item's follower is present, insert item before it.
- Insert item at end of list.
Examples:
[].merge([1, 2]) # => [1, 2]
['a'].merge([1, 2]) # => ['a', 1, 2]
[1, 'a'].merge([1, 2]) # => [1, 2, 'a']
[1, 'a'].merge([3, 1, 2]) # => [3, 1, 2, 'a']
Hash
Hash#to_uri
Returns URL-encoded string of uri params. Examples:
{:some => :value, :another => "speciál"}.to_uri # => "some=value&another=speci%C3%A1l"
{:some => {:nested => :thing}}.to_uri # => "some[nested]=thing"
Hash#only
Returns a copy of self including only the given keys. Example:
{:name => "Rodrigo", :age => 21}.only(:name) # => {:name => "Rodrigo"}
Hash#except
Returns a copy of self including all but the given keys. Example:
{:name => "Rodrigo", :age = 21}.except(:name) # => {:age => 21}
String
String#latinize
Returns a string without exotic chars. Examples:
"Hola señor, ¿cómo está?".latinize # => "Hola senor, como esta?"
"Ähre, wem Ähre gebührt.".latinize # => "AEhre, wem AEhre gebuehrt."
String#permalink
Returns a string that may be used as permalink. Example:
"Hola señor, ¿cómo está?".permalink # => "hola-senor-como-esta"
Copyright
Copyright (c) 2010-2019 Andre Pankratz. See LICENSE for details.