trying
A poor man's pipes
% echo "gem 'trying'" | Gemfile
% gem install
Usage
String.include()
Range.include()
stream = (0..10). do
to_a
lazy
map { |x| x ** 2 }
first(10) # => [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
end
truth = "Foobar". do # => 42
chars # "Foobar" -> ["F", "o", "o", "b", "a", "r"]
map(&:upcase) # %w(F o o b a r) -> %w(F O O B A R)
map(&:ord) # %w(F O O B A R) -> [70, 79, 79, 66, 65, 82]
reduce(0, :+) # [70, 79, 79, 66, 65, 82] -> 441
# with changes the value by block
with { |n| n * Math::PI } # 441 -> 1385.4423602330987
# tap leaves the callee untouched
tap { |n| puts n } # 1385.44... -> (Side effect), still 1385.44...
# with also swallows errors
with { |n| fail if n > 1380 } # 1385.44... -> RuntimeError
# failed? checks for any swallowed errors
failed? { :recover } # => RuntimeError -> :recover
# failed? accepts specific error classes
failed?(StandardError) { :nope } # :recover -> :recover
eql?(:recover) # :recover -> true
nil? # true -> false
to_s # false -> "false"
length # "false" -> 5
with { |n| (n..n + 20) } # 5 -> 5..25
to_a # 5..25 -> [5, 6, ..., 25]
lazy # [5, ..., 25] -> #<Enumerator::Lazy: ...>
collect { |x| x ** 2 } # #<Enumerator::Lazy: ...> -> #<Enumerator::Lazy: ...>
first(5) # #<Enumerator::Lazy: ...> -> [25, 36, 49, 64, 81]
reduce(1, &:*) # => [25, 36, 49, 64, 81] -> 228614400
public_send(:/, 5443200) # 228614400 => 42
end