💡 About
Class's methods convenient for scripting port from Crystal.
📥 Installation
Install the gem and add to the application's Gemfile by executing:
bundle add crystal_goodies
If bundler is not being used to manage dependencies, install the gem by executing:
gem install crystal_goodies
⌨️ Usage
In Ruby do:
require 'crystal_goodies'
Only require methods to access through module CrystalGoodies
.
require 'crystal_goodies/object'
require 'crystal_goodies/integer'
require 'crystal_goodies/string'
require 'crystal_goodies/array'
require 'crystal_goodies/enumerable'
Only require class extensions.
require 'crystal_goodies/object/extensions'
require 'crystal_goodies/integer/extensions'
require 'crystal_goodies/string/extensions'
require 'crystal_goodies/array/extensions'
require 'crystal_goodies/enumerable/extensions'
Object
in?(*collection) -> true, false
Returns true
if self
is included in the collection argument.
10.in?(0..100) # => true
10.in?(0, 1, 10) # => true
10.in?(:foo, :bar) # => false
Integer
divisible_by?(num) -> true, false
Returns true
if self
is divisible by num.
6.divisible_by? 3 # => true
6.divisible_by? 2 # => true
5.divisible_by? 3 # => false
to(limit) { |i| ... } -> self
Call upto
or downto
depend on the limit.
String
camelcase(options, lower: false) -> String
Converts underscores to camelcase boundaries.
If lower is true, lower camelcase will be returned (the first letter is downcased).
"eiffel_tower".camelcase # => "EiffelTower"
"empire_state_building".camelcase(lower: true) # => "empireStateBuilding"
titleize(options) -> String
Returns a new String
with the first letter after any space converted to uppercase and every
other letter converted to lowercase.
"hEllO tAb\tworld".titleize # => "Hello Tab\tWorld"
" spaces before".titleize # => " Spaces Before"
"x-men: the last stand".titleize # => "X-men: The Last Stand"
underscore(options) -> String
Converts camelcase boundaries to underscores.
"DoesWhatItSaysOnTheTin".underscore # => "does_what_it_says_on_the_tin"
"PartyInTheUSA".underscore # => "party_in_the_usa"
"HTTP_CLIENT".underscore # => "http_client"
"3.14IsPi".underscore # => "3.14_is_pi"
dasherize(options) -> String
Converts camelcase boundaries to kebabcase.
"DoesWhatItSaysOnTheTin".dasherize # => "does-what-it-says-on-the-tin"
"PartyInTheUSA".dasherize # => "party-in-the-usa"
"HTTP_CLIENT".dasherize # => "http-client"
"3.14IsPi".dasherize # => "3.14-is-pi"
blank? -> true, false
Returns true
if this string consists exclusively of unicode whitespace.
"".blank? # => true
" ".blank? # => true
" a ".blank? # => false
presence -> self, nil
Returns self
unless #blank?
is true
in which case it returns nil
.
"a".presence # => "a"
"".presence # => nil
" ".presence # => nil
" a ".presence # => " a "
delete_at(...) -> String
Returns a new string that results from deleting characters with slice
.
to(limit, exclusive = false) -> self
Call upto
or downto
depend on the limit.
Array
skip(count) -> Array
Returns an Array
with the first count elements removed
from the original array.
If count is bigger than the number of elements in the array, returns an empty array.
[1, 2, 3, 4, 5, 6].skip(3) # => [4, 5, 6]
truncate(...) -> elements
Removes all elements except the count or less (if there aren't enough)
elements starting at the given start index. Returns self
.
Negative values of start count from the end of the array.
Raises IndexError
if the start index is out of range.
Raises ArgumentError
if count is negative.
a = [0, 1, 4, 9, 16, 25]
a.truncate(2, 3) # => [4, 9, 16]
a # => [4, 9, 16]
Or removes all elements except those within the given range. Returns self
.
a = [0, 1, 4, 9, 16, 25]
a.truncate(1..-3) # => [1, 4, 9]
a # => [1, 4, 9]
Enumerable
empty? -> true, false
Returns true
if self
is empty, false
otherwise.
[].empty? # => true
[1].empty? # => false
compact_map { ... } -> Array
Returns an Array
with the results of running the block against each element
of the collection, removing nil
values.
["Alice", "Bob"].map { |name| name.match(/^A./) } # => [#<MatchData "Al">, nil]
["Alice", "Bob"].compact_map { |name| name.match(/^A./) } # => [#<MatchData "Al">]
in_groups_of(size, filled_up_with = nil) -> Array
Returns an Array
with chunks in the given size, eventually filled up
with given value or nil
.
[1, 2, 3].in_groups_of(2, 0) # => [[1, 2], [3, 0]]
[1, 2, 3].in_groups_of(2) # => [[1, 2], [3, nil]]
skip_while { ... } -> Array
Skips elements up to, but not including, the first element for which
the block is falsey, and returns an Array
containing the remaining elements.
[1, 2, 3, 4, 5, 0].skip_while { _1 < 3 } # => [3, 4, 5, 0]
index_by { ... } -> Hash
Converts an Enumerable
to a Hash
by using the value returned by the block
as the hash key.
Be aware, if two elements return the same value as a key one will override
the other. If you want to keep all values, then you should probably use
group_by
instead.
["Anna", "Ary", "Alice"].index_by(&:size)
# => {4 => "Anna", 3 => "Ary", 5 => "Alice"}
["Anna", "Ary", "Alice", "Bob"].index_by(&:size)
# => {4 => "Anna", 3 => "Bob", 5 => "Alice"}
tally_by(hash = {}) { ... } -> Hash
Tallies the collection. Accepts a hash to count occurrences.
The value corresponding to each element must be an integer.
Returns hash where the keys are the
elements and the values are numbers of elements in the collection
that correspond to the key after transformation by the given block.
hash = {}
words = ["Crystal", "Ruby"]
words.each { |word| word.chars.tally_by(hash, &:downcase) }
hash # => {"c" => 1, "r" => 2, "y" => 2, "s" => 1, "t" => 1, "a" => 1, "l" => 1, "u" => 1, "b" => 1}
min_of? { ... } -> min_element
Returns the element for which the passed block returns with the maximum value.
It compares using >
so the block must return a type that supports that method
["Alice", "Bob"].max_by(&:size) # => "Alice"
Return nil
if the collection is empty.
max_of? { ... } -> max_element
Returns the element for which the passed block returns with the minimum value.
It compares using <
so the block must return a type that supports that method
["Alice", "Bob"].min_by(&:size) # => "Bob"
Return nil
if the collection is empty.
minmax_of? { ... } -> [min_element, max_element]
Returns a Array
with both the minimum and maximum values according to the passed block.
["Alice", "Bob", "Carl"].minmax_by(&:size) # => ["Bob", "Alice"]
Return [nil, nil]
if the collection is empty.
💌 Credits