{"id":18111597,"url":"https://github.com/afaur/ruby-nicefn","last_synced_at":"2025-06-29T15:06:56.791Z","repository":{"id":56885516,"uuid":"152514030","full_name":"afaur/ruby-nicefn","owner":"afaur","description":"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.","archived":false,"fork":false,"pushed_at":"2018-10-15T09:04:17.000Z","size":31,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-12T22:18:40.645Z","etag":null,"topics":["functions","methods","one-liner","ruby","rubygem","rubygems"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/afaur.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-11T01:42:54.000Z","updated_at":"2018-10-15T09:04:18.000Z","dependencies_parsed_at":"2022-08-21T00:20:53.506Z","dependency_job_id":null,"html_url":"https://github.com/afaur/ruby-nicefn","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afaur%2Fruby-nicefn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afaur%2Fruby-nicefn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afaur%2Fruby-nicefn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afaur%2Fruby-nicefn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/afaur","download_url":"https://codeload.github.com/afaur/ruby-nicefn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247452582,"owners_count":20941129,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functions","methods","one-liner","ruby","rubygem","rubygems"],"created_at":"2024-11-01T01:06:50.789Z","updated_at":"2025-04-06T08:21:02.378Z","avatar_url":"https://github.com/afaur.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Gem Version](https://badge.fury.io/rb/nicefn.svg)](https://rubygems.org/gems/nicefn)\n[![Gem Downloads](https://ruby-gem-downloads-badge.herokuapp.com/nicefn?color=brightgreen\u0026type=total)](https://rubygems.org/gems/nicefn)\n[![Build Status](https://travis-ci.org/afaur/ruby-nicefn.svg?branch=master)](https://travis-ci.org/afaur/ruby-nicefn)\n[![Maintainability](https://api.codeclimate.com/v1/badges/6e10f0a9ac5b168e8821/maintainability)](https://codeclimate.com/github/afaur/ruby-nicefn/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/6e10f0a9ac5b168e8821/test_coverage)](https://codeclimate.com/github/afaur/ruby-nicefn/test_coverage)\n\n### Overview\nHere's a way to write functions (with visibility modification) in a single line of ruby.\n```rb\n  ...\n  fp(:priv)  { |greet| puts greet }\n  ...\n```\nThis will automatically declare a function with private visibility. Which will\nmake it like you had actually wrote:\n```rb\n  ...\n  private\n\n  def priv(greet)\n    puts greet\n  end\n  ...\n```\n\n### Example (Main Scope)\nIf you want to write short one-liners in the main scope you need not add this gem\nto your project. It is much quicker and simpler to do:\n```rb\nalias fn define_singleton_method\n\nfn(:test) { puts “hello” }\n\ntest # \u003c= prints \"hello\" to stdout\n```\n\n### Example (Regular Classes)\nProvided below is an example class with `public`, `private`, and `protected` methods:\n```rb\nclass Inst\n  attr_writer :person\n\n  def self.set_klass_property(value)\n    @@klass_property = value\n  end\n\n  def self.print_klass_property()\n    puts @@klass_property\n  end\n\n  def test_priv(greet)\n    priv greet\n  end\n\n  def test_share(greet, inst)\n    inst.share greet\n  end\n\n  private\n\n  def priv(greet)\n    puts \"#{greet} #{@person}\"\n  end\n\n  protected\n\n  def share(greet)\n    puts \"#{greet} #{@person}\"\n  end\nend\n```\nIf we use `nicefn` on this class we can eliminate more than 12 lines of code (if\nwe add spaces around private and protected like rubocop suggests) inside of the\nclass definition. This is because `private` and `protected` are handled by\ndifferent functions (like `defp` in `elixir`).\n\n### After Adding Nicefn::Inst\n```rb\nrequire 'nicefn'\n\nclass Inst\n  extend Nicefn::Inst\n  attr_writer :person\n\n  cm(:set_klass_property)   { |value| @@klass_property = value }\n  cm(:print_klass_property) { puts @@klass_property }\n\n  fn(:test_priv)  { |greet| priv greet }\n  fn(:test_share) { |greet, inst| inst.share greet }\n\n  fp(:priv)  { |greet| puts \"#{greet} #{@person}\" }\n\n  fs(:share) { |greet| puts \"#{greet} #{@person}\" }\nend\n```\nCalling `fn` with a function `name` and a block will give you a public method.\n(**Since version 0.1.1**) Class methods are created using the `cm` function.\nIf you call `fp` you will get a `private` method, and `fs` will set a\n`protected` (shared) method.\n\n### Example (Singleton Classes)\nProvided below is an example of a `module` that is made a `singleton class` by using\n`extend self`.\n```rb\nmodule Sing\n  extend self\n  attr_writer :person\n\n  def test_priv(greet)\n    priv greet\n  end\n\nprivate\n  def priv(greet)\n    puts \"#{greet} #{@person}\"\n  end\nend\n```\nAfter we add `include Nicefn::Sing` to the module we can eliminate the need to\nextend self as `Nicefn::Sing` will do it for us.\n\n### After Adding Nicefn::Sing\n```rb\nrequire 'nicefn'\n\nmodule Sing\n  include Nicefn::Sing\n  attr_writer :person\n\n  fn(:test_priv) { |greet| priv greet }\n  fp(:priv)      { |greet| puts \"#{greet} #{@person}\" }\nend\n```\nCalling `fn` with a function `name` and a block will give you a public method.\nIf you call `fp` you will get a `private` method. Since singletons classes can\nonly act as one instance 'fs' is not a provided option.\n\n## Install Gem\nYou can run `bundle add nicefn --version '~\u003e 0.1.0'`, or manually add a line\nindicating how you would like to fetch the `gem` to your `Gemfile`:\n```rb\n...\n# Download latest nicefn from default source\ngem 'nicefn'\n\n# Download nicefn from default source with version constraints\ngem 'nicefn', '~\u003e 0.1.1'\n\n# Download nicefn from git with a specific version\ngem 'nicefn', git: 'https://github.com/afaur/ruby-nicefn', tag: 'v0.1.1'\n...\n```\n\n## Project Structure\nRunning `make` will default to running the tests inside `tst` folder against the\nexamples inside the `exa` folder.\n\n## How To Use\nAdd `extend Nicefn::Inst` to the top of classes. You can also use `include\nNicefn::Sing` in a `module` to make it a singleton class with `nicefn` methods.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafaur%2Fruby-nicefn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fafaur%2Fruby-nicefn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafaur%2Fruby-nicefn/lists"}