Skip to content

steppat/named_parameter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Named Parameter v0.0.1

Description

That you ever dream with named parameter in Ruby? Well, you doesn't have to pray to Ruby's 2.0 have this, just install this gem and have fun!

How it works

Just extend the module NamedParameter in your class/module and use the method 'named' before define your method, when you call it, use a hash that's key is the parameter name. See this example:

class People
  extend NamedParameter
  
  named def say(phrase)
    puts "People says: #{phrase}"
  end
end

People.new.say phrase: "Awesome!"

Or maybe you want to define an optional parameter, no problem!

class People
  extend NamedParameter
  
  named def say(phrase="I'm mute")
    puts "People says: #{phrase}"
  end
end

People.new.say

Multiple arguments? Of course!

class Point
  extend NamedParameter
  
  named def move_to(x,y,z=0)
    puts "Moving to [#{x},#{y},#{z}]"
  end
end

Point.new.move_to(y: 30,x: 50)

Use in production?

You (maybe):"Oh! Magic! I'll use in my production projects!" Me: Wait! Before use this in production, you have to know that gem use the method_added callback, so if you want to use named parameter and this callback in the same class, you have to use around alias spell.

How Install

Install the gem:

gem install named_parameter

About

Allows named parameter in Ruby

Resources

Stars

Watchers

Forks

Packages

No packages published