Code Monkey home page Code Monkey logo

afaur / ruby-nicefn Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 31 KB

Elixir and javascript have the capability of making good looking one liners, but what about Ruby? We can definitely make an awful looking one by adding a ';'. If you want to start defining some better looking one-liners then add the 'nicefn' gem to your project. Since the implementation files are small and this project has no required deps. You should also feel free to copy and paste the implementation directly into your project in an effort to avoid extra gems.

License: The Unlicense

Makefile 25.23% Ruby 64.80% Shell 9.98%
one-liner rubygems rubygem ruby functions methods

ruby-nicefn's Introduction

Gem Version Gem Downloads Build Status Maintainability Test Coverage

Overview

Here's a way to write functions (with visibility modification) in a single line of ruby.

  ...
  fp(:priv)  { |greet| puts greet }
  ...

This will automatically declare a function with private visibility. Which will make it like you had actually wrote:

  ...
  private

  def priv(greet)
    puts greet
  end
  ...

Example (Main Scope)

If you want to write short one-liners in the main scope you need not add this gem to your project. It is much quicker and simpler to do:

alias fn define_singleton_method

fn(:test) { puts “hello” }

test # <= prints "hello" to stdout

Example (Regular Classes)

Provided below is an example class with public, private, and protected methods:

class Inst
  attr_writer :person

  def self.set_klass_property(value)
    @@klass_property = value
  end

  def self.print_klass_property()
    puts @@klass_property
  end

  def test_priv(greet)
    priv greet
  end

  def test_share(greet, inst)
    inst.share greet
  end

  private

  def priv(greet)
    puts "#{greet} #{@person}"
  end

  protected

  def share(greet)
    puts "#{greet} #{@person}"
  end
end

If we use nicefn on this class we can eliminate more than 12 lines of code (if we add spaces around private and protected like rubocop suggests) inside of the class definition. This is because private and protected are handled by different functions (like defp in elixir).

After Adding Nicefn::Inst

require 'nicefn'

class Inst
  extend Nicefn::Inst
  attr_writer :person

  cm(:set_klass_property)   { |value| @@klass_property = value }
  cm(:print_klass_property) { puts @@klass_property }

  fn(:test_priv)  { |greet| priv greet }
  fn(:test_share) { |greet, inst| inst.share greet }

  fp(:priv)  { |greet| puts "#{greet} #{@person}" }

  fs(:share) { |greet| puts "#{greet} #{@person}" }
end

Calling fn with a function name and a block will give you a public method. (Since version 0.1.1) Class methods are created using the cm function. If you call fp you will get a private method, and fs will set a protected (shared) method.

Example (Singleton Classes)

Provided below is an example of a module that is made a singleton class by using extend self.

module Sing
  extend self
  attr_writer :person

  def test_priv(greet)
    priv greet
  end

private
  def priv(greet)
    puts "#{greet} #{@person}"
  end
end

After we add include Nicefn::Sing to the module we can eliminate the need to extend self as Nicefn::Sing will do it for us.

After Adding Nicefn::Sing

require 'nicefn'

module Sing
  include Nicefn::Sing
  attr_writer :person

  fn(:test_priv) { |greet| priv greet }
  fp(:priv)      { |greet| puts "#{greet} #{@person}" }
end

Calling fn with a function name and a block will give you a public method. If you call fp you will get a private method. Since singletons classes can only act as one instance 'fs' is not a provided option.

Install Gem

You can run bundle add nicefn --version '~> 0.1.0', or manually add a line indicating how you would like to fetch the gem to your Gemfile:

...
# Download latest nicefn from default source
gem 'nicefn'

# Download nicefn from default source with version constraints
gem 'nicefn', '~> 0.1.1'

# Download nicefn from git with a specific version
gem 'nicefn', git: 'https://github.com/afaur/ruby-nicefn', tag: 'v0.1.1'
...

Project Structure

Running make will default to running the tests inside tst folder against the examples inside the exa folder.

How To Use

Add extend Nicefn::Inst to the top of classes. You can also use include Nicefn::Sing in a module to make it a singleton class with nicefn methods.

ruby-nicefn's People

Contributors

afaur avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.