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
...
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
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
).
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.
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.
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.
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'
...
Running make
will default to running the tests inside tst
folder against the
examples inside the exa
folder.
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.