Traversing the fields of a structure, matching and assigning values.
-
Add the dependency to your
shard.yml
:dependencies: matching: github: Hentioe/matching.cr
-
Run
shards install
require "matching"
class Person
property name : String
property age : Int32
property hobbies : Array(String)
def initialize(@name, @age, @hobbies)
end
end
p1 = Person.new("John", 18, ["Swimming"])
p2 = Person.new("Mary", 21, [] of String)
name : String? = nil
Matching.matching(p1, name: name, age: 18, hobbies: ["Swimming"])
pp name # => "John"
Matching.matching(p1, name: name, age: 21)
pp name # => nil
age : Int32? = nil
Matching.matching(p2, name: "Mary", age: age)
pp age # => 21
- Fork it (https://github.com/Hentioe/matching/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Hentioe - creator and maintainer