lab42_core

Build Status Code Climate Test Coverage Gem Version

Simple Ruby Core Module Extensions (for more see lab42_more)

Array

Can be used after require 'lab42/core' or require 'lab42/core/array'

flatten_once

    [].flatten_once.assert.empty?

    [[2, {a: 3}, [4]], {a: 5}].flatten_once.assert ==
      [2, {a: 3}, [4], {a: 5}]

For details see the corresponding QED demo.

Dir

Can be used after require 'lab42/core' or require 'lab42/core/dir'

  Dir.files "**/*" do | partial_path, full_path |
  end

If only the relative or absolute pathes are needed there are the two variations avaiable:

    Dir.abs_files ...
    Dir.rel_files ...

For details see the corresponding QED demo.

Enumerable

grep2

  enum.grep2 expr # ===>
  enum.partition{ |ele| expr === ele }

to_proc

And also Enumerable#to\_proc as e.g.

    counter = (1..3).to_proc
    counter.().assert == 1
    counter.().assert == 2
    counter.().assert == 3
    StopIteration.assert.raised? do
      counter.()
    end

For details see the corresponding QED demo.

File

expand_local_path

expand_local_path to get rid of the __FILE__ inside expand_path.

For details see the corresponding QED demo.

if_readable

    File.if_readable 'some_file' do | file |  # openes file as readable

    end

if_writeable

Hash

  {a: 42, b: 43}.only :a, :c # ===> {a: 42}

For details see the corresponding QED demo.

Fn

Can be used after require 'lab42/core/array' only.

Might be moved into gem lab42_more in the future .

API will remain the same, require will change to require 'lab42_more/fn'

fn like function

    Dir.files [APP_ROOT, 'spec', 'support', '**', '*.rb'], Kernel.fn.require

    Dir.files( %w{.. assets ** *.txt} ).sort_by &File.fn.mtime

fm like function/method

    %w{ alpha beta gamma delta }.sort_by &String.fm.size

N.B. This only works because the object behind the scenes of Class#fm knows how to bind upon call, once it has been transformed by #to_proc

For details see the corresponding QED demo.

Object

Backport of #itself for versions < 2.2

OpenObject

Immutable Open Objects

  x = OpenObject.new a: 42
  x.a.assert   == 42
  x[:a].assert == 42

Immutability

All modifications just return a new instance.

  x = OpenObject.new a: 42, b: 43

  y = x.update a: 44
  y.to_hash.assert == {a: 44, b: 43}
  x.a.assert       ==  42

For details see the corresponding QED demo.