Skip to content

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 …

License

Notifications You must be signed in to change notification settings

afaur/ruby-nicefn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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 …

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published