Bringing some of the Ruby methods you miss to JavaScript.
If you're like me and love Ruby, you might miss some of the default class methods that Ruby provides that JavaScript doesn't. Take for example Array#last. In Ruby:
# Ruby
['one', 'dos', 'trois'].last
#=> "trois"
Nice, easy, simple, clear, obvious. In JavaScript we instead have to:
// Javascript
arr = ['one', 'dos', 'trois']
arr[arr.lenth - 1]
//=> "trois"
Rubyfy adds a last
method to the Array prototype chain allowing you to:
// JavaScript
['one', 'dos', 'trois'].last()
//=> "trois"
Similar to Ruby Array#any?
, returns true
or false
:
[1, 2, 3].any() //=> true
[].any() //=> false
Similar to Ruby Array#compact
, removes all null
and undefined
from an array:
['so', null, 'much', undefined, 'waste'].compact()
//=> ["so", "much", "waste"]
Similar to Ruby Array#empty?
, returns either true
or false
:
[].is_empty() //=> true
[1, 2, 3].is_empty() //=> false
Similar to Ruby Array#first
, returns the first item in an array:
['blue', 'red', 'green'].first()
//=> "blue"
NOTE: Unlike Ruby, first
does NOT take any arguments, so you can't do [1, 2, 3].first(2)
for example.
Similar to Ruby Array#last
, returns the last item in an array:
['uno', 'dos', 'tres'].last()
//=> "tres"
NOTE: Unlike Ruby, last
does NOT take any arguments, so you can't do [1, 2, 3].last(2)
for example.
Similar to Ruby Hash#any?
, returns either true
or false
:
obj = {kingdom: 'Fungi', division: 'Basidiomycota'}
obj.any()
//=> true
obj = {}
obj.any()
//=> false
Similar to Ruby Hash#empty?
, returns either true
or false
:
obj = {}
obj.is_empty()
//=> true
obj = {model: 'Mustang', horsepower: 480}
obj.is_empty()
//=> false
Similar to Ruby Hash#keys
, returns an array of the keys:
obj = {we: 100, love: 200, ruby: 300}
obj.keys()
//=> ["we", "love", "ruby"]
Similar to Ruby Hash#values
, returns an array of the values:
obj = {a: 'you', b: 'are', c: 'welcome', d: 'here'}
obj.vals()
//=> ["you", "are", "welcome", "here"]
Similar to Ruby String#capitalize
, returns a string with the first letter capitalized and all others lowercased:
'not yelling'.capitalize() //=> "Not yelling"
'NOT YELLING'.capitalize() //=> "Not yelling"
'123ABC'.capitalize() //=> "123abc"
Similar to Ruby String#downcase
, returns a string with all chars lowercase:
'WHISPER IN MY EAR'.downcase()
//=> "whisper in my ear"
NOTE: Yup, this is exactly the same as the built in JS toLowerCase
method.
Similar to Ruby String#blank?
, returns either true
or false
:
''.is_blank() //=> true
'Not Blank'.is_blank() //=> false
Rubyfy also comes with a handy R
shorthand that can be used in place of Rubyfy
:
R.downcase('HELLO') //=> 'hello'
R.vals({a: 100, b: 200}) //=> [100, 200]
NOTE: If you already have a global var named R
defined in your application, no worries, Rubyfy will check before overriding yours.
If you prefer to turn the R
shorthand option off all together you can:
Rubyfy.no_r = true
NOTE: Please set this option BEFORE including the Rubyfy library.
Some folks don't like the idea of opening up built-in classes and adding new functions. If you don't like the sound of that either, that's great! You can still use Rubyfy, too. Just make sure to set:
Rubyfy.no_prototypes = true
NOTE: Please set this option BEFORE including the Rubyfy library.
You can still access all the Rubyfy goodness directly by calling the above methods on Rubyfy
- or R
- explicitly:
Rubyfy.any([1,2,3]) //=> true
Rubyfy.keys({a: 100, b: 200}) //=> ["a", "b"]
You may be saying to yourself "wow, there are a whole bunch of methods in Ruby that I wish I had in JavaScript that aren't even IN this gem" and you wouldn't be wrong. Although the code here is tested and working, this gem is still very much a WORK IN PROGRESS as far as adding methods go. So far I've included some of the things I really miss while coding on my own JavaScript projects, but clearly the list provided in Rubyfy
is not nearly exhaustive. Help make Rubyfy more complete by submitting a pull request with your favorite missing methods! (And how much do you miss method_missing
amiright?!)
MIT - Free and open for all. Plz contribue!