extensions

Version, currently master branch1 version
  • master branchlatestMar 3, 2018

github.com/watzon/extensions

Helpful extensions to existing Crystal classes and modules

3 stars
1 dependent
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  extensions:
    github: watzon/extensions
    branch: master

master is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
0.24.1
License
MIT
Author
Chris Watson

Dependencies

Development Dependencies

  • spec2*github: watzon/spec2.cr, branch: masterdev

README

Crystal Extensions

Lots of good ideas don't make it in to the Crystal standard library. This is a community controlled repository where those good ideas can live and become a part of other shards. Think of it as the Ruby communities activesupport/core_ext, only for Crystal.

Installation

Add this to your application's shard.yml:

dependencies:
  extensions:
    github: watzon/extensions

Documentation

Enumerable(T)

take(count : Int)

Returns the first n elements of an enumerable and returns an array with the results. Functionally the same as first, but take requires an argument.

Hash(K, V)

dig(*args)

Extracts the nested value specified by the sequence of key objects by calling dig at each step, returning nil if any intermediate step is nil.

hash = {"a" => {"b" => {"c" => "c"}}}

hash.dig("a", "b", "c")      # => "c"
hash.dig("a", "b", "c", "d") # => nil

map_keys(&block)

Returns a new hash with all keys converted using the block operation. The block can change a type of keys.

hash = {:a => 1, :b => 2, :c => 3}
hash.map_keys { |key| key.to_s } # => {"A" => 1, "B" => 2, "C" => 3}

map_values(&block)

Returns a new hash with the results of running block once for every value. The block can change a type of values.

hash = {:a => 1, :b => 2, :c => 3}
hash.map_values { |value| value + 1 } # => {:a => 2, :b => 3, :c => 4}

map_values!(&block)

Destructively transforms all values using a block. Same as map_values but modifies in place. The block cannot change a type of values.

hash = {:a => 1, :b => 2, :c => 3}
hash.map_values! { |value| value + 1 } # => {:a => 2, :b => 3, :c => 4}

Object

macro equalize(*args, other_class = nil, strict = false)

Creates a #== method for a class or module.

Example:

class SomeClass
  @id : Int32
  def initialize(@id)
  end
  equalize({:id, :id})
end

a = SomeClass.new(15)
b = SomeClass.new(15)
c = SomeClass.new(16)

puts a == b # => true
puts b == c # => false

Using strict:

class Base
  @id : Int32
  def initialize(@id)
  end
  equalize({:id, :id}, strict: true)
end

class SomeClass < Base
  equalize({:id, :id}, strict: true)
end

a = Base.new(15)
b = SomeClass.new(15)

puts a == b #=> false

macro alias_method(new_name, existing_method)

Creates a method that mirrors an existing method.

Example:

def send_tweet(text)
  puts text
end
alias_method :tweet, :send_tweet

tweet("Hello world")

Contributing

  1. Fork it ( https://github.com/watzon/extensions/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • watzon Chris Watson - creator, maintainer