Socket
Book a DemoInstallSign in
Socket

trying

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trying

0.0.1
bundlerRubygems
Version published
Maintainers
1
Created
Source

trying

A poor man's pipes

% echo "gem 'trying'" | Gemfile
% gem install trying

Usage

String.include(Trying)
Range.include(Trying)

stream = (0..10).trying do
  to_a
  lazy
  map { |x| x ** 2 }
  first(10) # => [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
end

truth = "Foobar".trying 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

FAQs

Package last updated on 22 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.