README.adoc in refinements-9.0.2 vs README.adoc in refinements-9.1.0

- old
+ new

@@ -181,10 +181,23 @@ [1, 2, 3].intersperse :a # [1, :a, 2, :a, 3] [1, 2, 3].intersperse :a, :b # [1, :a, :b, 2, :a, :b, 3] [1, 2, 3].intersperse %i[a b c] # [1, :a, :b, :c, 2, :a, :b, :c, 3] ---- +===== #many? + +Answers true if an array has more than one element. Can take a block which evaluates as truthy or +falsey. + +[source,ruby] +---- +[1, 2].many? # true +[1, 2, 3].many?(&:odd?) # true +[1].many? # false +[].many? # false +---- + ===== #maximum Answers the maximum extracted value from a collection of objects. [source,ruby] @@ -434,10 +447,23 @@ example = {a: {b: 1}} example.flatten_keys! # {a_b: 1} example # {a_b: 1} ---- +===== #many? + +Answers true if a hash has more than one element. Can take a block which evaluates as truthy or +falsey. + +[source,ruby] +---- +{a: 1, b: 2}.many? # true +{a: 1, b: 2, c: 2}.many? { |_key, value| value == 2 } # true +{a: 1}.many? # false +{}.many? # false +---- + ===== #recurse Recursively iterates over the hash and any hash value by applying the given block to it. Does not handle nested arrays, though. @@ -692,10 +718,32 @@ # When path doesn't exist. Pathname("/example.txt").delete # Errno::ENOENT ---- +===== #delete_prefix + +Deletes a path prefix and answers new pathname. + +[source,ruby] +---- +Pathname("a/path/example-test.rb").delete_prefix("example-") # Pathname("a/path/test.rb") +Pathname("example-test.rb").delete_prefix("example-") # Pathname("test.rb") +Pathname("example-test.rb").delete_prefix("miss") # Pathname("example-test.rb") +---- + +===== #delete_suffix + +Deletes a path suffix and answers new pathname. + +[source,ruby] +---- +Pathname("a/path/test-example.rb").delete_suffix("-example") # Pathname("a/path/test.rb") +Pathname("test-example.rb").delete_suffix("-example") # Pathname("test.rb") +Pathname("test-example.rb").delete_suffix("miss") # Pathname("test-example.rb") +---- + ===== #directories Answers all directories or filtered directories for current path. [source,ruby] @@ -1053,127 +1101,146 @@ whether the struct was constructed with positional or keyword arguments. [source,ruby] ---- Example = Struct.new :a, :b, :c -Example.with_keywords a: 1, b: 2, c: 3 # "#<struct a=1, b=2, c=3>" -Example.with_keywords a: 1 # "#<struct a=1, b=nil, c=nil>" -Example.with_keywords c: 1 # "#<struct a=nil, b=nil, c=1>" +Example.with_keywords a: 1, b: 2, c: 3 # #<struct a=1, b=2, c=3> +Example.with_keywords a: 1 # #<struct a=1, b=nil, c=nil> +Example.with_keywords c: 1 # #<struct a=nil, b=nil, c=1> Example = Struct.new :a, :b, :c, keyword_init: true -Example.with_keywords a: 1, b: 2, c: 3 # "#<struct a=1, b=2, c=3>" -Example.with_keywords a: 1 # "#<struct a=1, b=nil, c=nil>" -Example.with_keywords c: 1 # "#<struct a=nil, b=nil, c=1>" +Example.with_keywords a: 1, b: 2, c: 3 # #<struct a=1, b=2, c=3> +Example.with_keywords a: 1 # #<struct a=1, b=nil, c=nil> +Example.with_keywords c: 1 # #<struct a=nil, b=nil, c=1> ---- ===== .with_positions Answers a struct instance with given positional arguments regardless of whether the struct was constructed with positional or keyword arguments. [source,ruby] ---- Example = Struct.new :a, :b, :c -Example.with_positions 1, 2, 3 # "#<struct a=1, b=2, c=3>" -Example.with_positions 1 # "#<struct a=1, b=nil, c=nil>" +Example.with_positions 1, 2, 3 # #<struct a=1, b=2, c=3> +Example.with_positions 1 # #<struct a=1, b=nil, c=nil> Example = Struct.new :a, :b, :c, keyword_init: true -Example.with_positions 1, 2, 3 # "#<struct a=1, b=2, c=3>" -Example.with_positions 1 # "#<struct a=1, b=nil, c=nil>" +Example.with_positions 1, 2, 3 # #<struct a=1, b=2, c=3> +Example.with_positions 1 # #<struct a=1, b=nil, c=nil> ---- ===== #merge Merges multiple attributes without mutating itself and supports any object that responds to `#to_h`. +Works regardless of whether the struct is constructed with positional or keyword arguments. [source,ruby] ---- +example = Struct.new("Example", :a, :b, :c).new 1, 2, 3 other = Struct.new("Other", :a, :b, :c).new 7, 8, 9 -example = Struct.new(:a, :b, :c).new 1, 2, 3 -example.merge a: 10 # "#<struct a=10, b=2, c=3>" -example.merge a: 10, c: 30 # "#<struct a=10, b=2, c=30>" -example.merge a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>" -example.merge {a: 10, b: 20} # "#<struct a=10, b=20, c=3>" -example.merge other # "#<struct a=7, b=8, c=9>" -example # "#<struct a=1, b=2, c=3>" - -example = Struct.new(:a, :b, :c, keyword_init: true).new a: 1, b: 2, c: 3 -example.merge a: 10 # "#<struct a=10, b=2, c=3>" -example.merge a: 10, c: 30 # "#<struct a=10, b=2, c=30>" -example.merge {a: 10, b: 20} # "#<struct a=10, b=20, c=3>" -example.merge other # "#<struct a=7, b=8, c=9>" -example.merge a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>" -example # "#<struct a=1, b=2, c=3>" +example.merge a: 10 # #<struct Struct::Example a=10, b=2, c=3> +example.merge a: 10, c: 30 # #<struct Struct::Example a=10, b=2, c=30> +example.merge a: 10, b: 20, c: 30 # #<struct Struct::Example a=10, b=20, c=30> +example.merge other # #<struct Struct::Example a=7, b=8, c=9> +example # #<struct Struct::Example a=1, b=2, c=3> ---- ===== #merge! Merges multiple attributes while mutating itself and supports any object that responds to `#to_h`. +Works regardless of whether the struct is constructed with positional or keyword arguments. [source,ruby] ---- +example = Struct.new("Example", :a, :b, :c).new 1, 2, 3 other = Struct.new("Other", :a, :b, :c).new 7, 8, 9 -example = Struct.new(:a, :b, :c).new 1, 2, 3 -example.merge! a: 10 # "#<struct a=10, b=2, c=3>" -example.merge! a: 10, c: 30 # "#<struct a=10, b=2, c=30>" -example.merge! {a: 10, b: 20} # "#<struct a=10, b=20, c=3>" -example.merge! other # "#<struct a=7, b=8, c=9>" -example.merge! a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>" -example # "#<struct a=10, b=20, c=30>" - -example = Struct.new(:a, :b, :c, keyword_init: true).new a: 1, b: 2, c: 3 -example.merge! a: 10 # "#<struct a=10, b=2, c=3>" -example.merge! a: 10, c: 30 # "#<struct a=10, b=2, c=30>" -example.merge! {a: 10, b: 20} # "#<struct a=10, b=20, c=3>" -example.merge! other # "#<struct a=7, b=8, c=9>" -example.merge! a: 10, b: 20, c: 30 # "#<struct a=10, b=20, c=30>" -example # "#<struct a=10, b=20, c=30>" +example.merge! a: 10 # #<struct Struct::Example a=10, b=2, c=3> +example.merge! a: 10, c: 30 # #<struct Struct::Example a=10, b=2, c=30> +example.merge! other # #<struct Struct::Example a=7, b=8, c=9> +example.merge! a: 10, b: 20, c: 30 # #<struct Struct::Example a=10, b=20, c=30> +example # #<struct Struct::Example a=10, b=20, c=30> ---- ===== #revalue -Transforms values without mutating itself. An optional hash can be supplied to pinpoint and -transform specific attributes. In the event that a block isn't supplied, the struct will answer -itself since there is nothing to operate on. Behavior is the same regardless of whether the struct -is constructed using positional or keyword arguments. A positional struct is used in the examples -below but a keyword struct would work too. +Transforms values without mutating itself. An optional hash can be supplied to target specific +attributes. In the event that a block isn't supplied, the struct will answer itself since there is +nothing to operate on. Behavior is the same regardless of whether the struct is constructed using +positional or keyword arguments. Works regardless of whether the struct is constructed with +positional or keyword arguments. [source,ruby] ---- example = Struct.new("Example", :a, :b, :c).new 1, 2, 3 -example.revalue { |value| value * 2 } # "#<struct a=2, b=4, c=6>" -example.revalue(c: 2) { |previous, current| previous + current } # "#<struct a=1, b=2, c=5>" -example.revalue c: 2 # "#<struct a=1, b=2, c=3>" -example.revalue # "#<struct a=1, b=2, c=3>" -example # "#<struct a=1, b=2, c=3>" + +example.revalue { |value| value * 2 } # #<struct Struct::Example a=2, b=4, c=6> +example.revalue(c: 2) { |previous, current| previous + current } # #<struct Struct::Example a=1, b=2, c=5> +example.revalue c: 2 # #<struct Struct::Example a=1, b=2, c=3> +example.revalue # #<struct Struct::Example a=1, b=2, c=3> +example # #<struct Struct::Example a=1, b=2, c=3> ---- ===== #revalue! -Transforms values while mutating itself. An optional hash can be supplied to pinpoint and transform -specific attributes. In the event that a block isn't supplied, the struct will answer itself since -there is nothing to operate on. Behavior is the same regardless of whether the struct is constructed -using positional or keyword arguments. A positional struct is used in the examples below but a -keyword struct would work too. +Transforms values while mutating itself. An optional hash can be supplied to target specific +attributes. In the event that a block isn't supplied, the struct will answer itself since there is +nothing to operate on. Behavior is the same regardless of whether the struct is constructed using +positional or keyword arguments. Works regardless of whether the struct is constructed with +positional or keyword arguments. [source,ruby] ---- -Example = Struct.new :a, :b, :c +one = Struct.new("One", :a, :b, :c).new 1, 2, 3 +one.revalue! { |value| value * 2 } # #<struct Struct::One a=2, b=4, c=6> +one # #<struct Struct::One a=2, b=4, c=6> -example = Example[1, 2, 3] -example.revalue! { |value| value * 2 } # "#<struct a=2, b=4, c=6>" -example # "#<struct a=2, b=4, c=6>" +two = Struct.new("Two", :a, :b, :c).new 1, 2, 3 +two.revalue!(c: 2) { |previous, current| previous + current } # #<struct Struct::Two a=1, b=2, c=5> +two # #<struct Struct::Two a=1, b=2, c=5> -example = Example[1, 2, 3] -example.revalue!(c: 2) { |previous, current| previous + current } # "#<struct a=1, b=2, c=5>" -example # "#<struct a=1, b=2, c=5>" +three = Struct.new("Three", :a, :b, :c).new 1, 2, 3 +three.revalue! c: 2 # #<struct Struct::Three a=1, b=2, c=3> +three.revalue! # #<struct Struct::Three a=1, b=2, c=3> +three # #<struct Struct::Three a=1, b=2, c=3> +---- -example = Example[1, 2, 3] -example.revalue! c: 2 # "#<struct a=1, b=2, c=3>" -example.revalue! # "#<struct a=1, b=2, c=3>" -example # "#<struct a=1, b=2, c=3>" +===== #transmute + +Transmutes given enumerable by using the foreign key map and merging those key values into the +current struct while not mutating itself. Works regardless of whether the struct is constructed with +positional or keyword arguments. + +[source,ruby] +---- +a = Struct.new("A", :a, :b, :c).new 1, 2, 3 +b = Struct.new("B", :x, :y, :z).new 7, 8, 9 +c = {r: 10, s: 20, t: 30} + +a.transmute b, a: :x, b: :y, c: :z # #<struct Struct::A a=7, b=8, c=9> +a.transmute b, b: :y # #<struct Struct::A a=1, b=8, c=3> +a.transmute c, c: :t # #<struct Struct::A a=1, b=2, c=30> +a # #<struct Struct::A a=1, b=2, c=3> +---- + +===== #transmute! + +Transmutes given enumerable by using the foreign key map and merging those key values into the +current struct while mutating itself. Works regardless of whether the struct is constructed with +positional or keyword arguments. + +[source,ruby] +---- +a = Struct.new("A", :a, :b, :c).new 1, 2, 3 +b = Struct.new("B", :x, :y, :z).new 7, 8, 9 +c = {r: 10, s: 20, t: 30} + +a.transmute! b, a: :x, b: :y, c: :z # #<struct Struct::A a=7, b=8, c=9> +a.transmute! b, b: :y # #<struct Struct::A a=1, b=8, c=3> +a.transmute! c, c: :t # #<struct Struct::A a=1, b=2, c=30> +a # #<struct Struct::A a=7, b=8, c=30> ---- ==== Symbol ===== #call