SkillAgentSearch skills...

Rubyinjs

Makes Ruby like APIs available in JS Objects like Array and Strings

Install / Use

/learn @the-ruby-js-project/Rubyinjs
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Ruby In JS Project

example workflow

<img src="logo.png" alt="Rubyinjs Logo" width="200"/>

Enjoy Ruby like functions in jS Objects; make your life easier


Basic Usage

Note: since, no ? symbol is supported by Javascript therefore for methods like match? or include?, empty? use _ instead of ?.

After deployment the latest version of the ruby.js library will be available at https://the-ruby-js-project.github.io/rubyinjs/ruby.js

<script src="https://the-ruby-js-project.github.io/rubyinjs/ruby.js"></script>

<script>
    console.log("1234".to_i())                       // 1234
    console.log("1234.35".to_f())                    // 1234.35
    console.log("shiva bhusal".start_with_('shi'))   // true
    console.log("shiva bhusal".start_with_('hi'))    // false
</script>

in Rails Application

# Gemfile
gem 'rubyinjs'
// in application.js

//= require 'rubyinjs'

String methods

*(n)

"Ho! ".x(3)                             //=> "Ho! Ho! Ho! "

capitalize

"hello".capitalize()                    //=> "Hello"

casecmp?(str)

"abcdef".casecmp_("abcde")              //=> false
"aBcDeF".casecmp_("abcdef")             //=> true
"abcdef".casecmp_("abcdefg")            //=> false
"abcdef".casecmp_("ABCDEF")             //=> true

center


"hello".center(4)                       //=> "hello"
"hello".center(20)                      //=> "       hello        "
"hello".center(20, '123')               //=> "1231231hello12312312"

chr

"abcde".chr()                           //=> "a"

downcase


"hEllO".downcase()                      //=> "hello"

upcase

"hEllO".upcase()                        //=> "HELLO"

each_char {}

"hello".each_char(function(c){ console.log(c, ' ') })
    produces:

    h e l l o

empty?

"hello".empty_                          //=> false
" ".empty _                             //=> false
"".empty_                               //=> true

end_with?(str)

"hello".end_with_("ello")               //=> true

# returns true if one of the +suffixes+ matches.
"hello".end_with_("heaven", "ello")     //=> true
"hello".end_with_("heaven", "paradise") //=> false

gsub(patern, replacement_str)

"hello".gsub(/[aeiou]/, '*')            //=> "h*ll*"
"hello".gsub(/([aeiou])/, '<\1>')       //=> "h<e>ll<o>"

include?(str)

"hello".include_("lo")                  //=> true
"hello".include_("ol")                  //=> false
"hello".include_('h')                   //=> true

index

"hello".index('e')                      //=> 1
"hello".index('lo')                     //=> 3
"hello".index('a')                      //=> nil
"hello".index('e')                      //=> 1

lines

"hello\nworld\n".lines()                //=> ["hello\n", "world\n"]
"hello  world".lines(' ')               //=> ["hello ", " ", "world"]
"hello\nworld\n".lines()                //=> ["hello", "world"]

lstrip

"  hello  ".lstrip()                    //=> "hello  "
"hello".lstrip()                        //=> "hello"

rstrip

"  hello  ".rstrip()                    //=> "  hello"
"hello".rstrip()                        //=> "hello"

match?(regex)

"Ruby".match_(/R.../)                   //=> true
"Ruby".match_(/R.../, 1)                //=> false
"Ruby".match_(/P.../)                   //=> false

succ

"abcd".succ()                           //=> "abce"
"THX1138".succ()                        //=> "THX1139"
"<<koala>>".succ()

next

"abcd".next()                           //=> "abce"
"THX1138".next()                        //=> "THX1139"
"<<koala>>".next()                      //=> "<<koalb>>"

ord

"a".ord()                               //=> 49

prepend

// Non mutative
a = "!"
a.prepend("hello ", "world")            //=> "hello world!"
a.prepend("hello ", "world", 'shiva')   //=> "hello world shiva!"

reverse

"stressed".reverse()                    //=> "desserts"

scan(pattern)

a = "cruel world"
a.scan(/\w+/)                           //=> ["cruel", "world"]
a.scan(/.../)                           //=> ["cru", "el ", "wor"]
a.scan(/(...)/)                         //=> [["cru"], ["el "], ["wor"]]
a.scan(/(..)(..)/)                      //=> [["cr", "ue"], ["l ", "wo"]]

size

"cruel world".size()  //=> 11

start_with?(str)

"hello".start_with_("hell")               //=> true
"hello".start_with_(/H/i)                 //=> true
// returns true if one of the prefixes matches.
"hello".start_with_("heaven", "hell")     //=> true
"hello".start_with_("heaven", "paradise") //=> false

sub(pattern)

"hello".sub(/[aeiou]/, '*')             //=> "h*llo"

swapcase

"Hello".swapcase()                      //=> "hELLO"
"cYbEr_PuNk11".swapcase()               //=> "CyBeR_pUnK11"

to_f

"123.45e1".to_f()                       //=> 1234.5
"45.67 degrees".to_f()                  //=> 45.67
"thx1138".to_f()                        //=> 0.0

to_i

"12345".to_i()                          //=> 12345
"99 red balloons".to_i()                //=> 99
"0a".to_i()                             //=> 0
"0a".to_i(16)                           //=> 10
"hello".to_i()                          //=> 0
"1100101".to_i(2)                       //=> 101
"1100101".to_i(8)                       //=> 294977
"1100101".to_i(10)                      //=> 1100101
"1100101".to_i(16)                      //=> 17826049

Array Methods

at(index)

[1, 2, 3, 4, 5, 6].at(1)  //=> 2

fetch(index, fallback)

arr = ['a', 'b', 'c', 'd', 'e', 'f']
arr.fetch(100)                          //=> IndexError: index 100 outside of array bounds: -6...6
arr.fetch(100, "oops")                  //=> "oops"
 

take(count)

arr = ['a', 'b', 'c', 'd', 'e', 'f']
arr.take(1)                             //=> ['a']
arr.take(2)                             //=> ['a', 'b']

first(count)

arr = ['a', 'b', 'c', 'd', 'e', 'f']
arr.first()                             //=> 'a'
arr.first(2)                            //=> ['a', 'b']

last(count)

arr = ['a', 'b', 'c', 'd', 'e', 'f']
arr.last                                //=> 'f'
arr.last(2)                             //=> ['e', 'f']

count

browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
browsers.length //=> 5
browsers.count //=> 5

include?(item)


browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
browsers.include_('Konqueror')          //=> false

empty?

browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
browsers.empty_ //=> false

insert(index, item)

a = %w{ a b c d }
a.insert(2, 99)                         //=> ["a", "b", 99, "c", "d"]
a.insert(-2, 1, 2, 3)                   //=> ["a", "b", 99, "c", 1, 2, 3, "d"]

count

[ 1, 2, 3, 4, 5 ].size()                //=> 5

collect

 1, 2, 3, 4, 5 ].collect {}             //=> 5

pop

a = [ "a", "b", "c", "d" ]
a.pop                                   //=> "d"
a.pop(2)                                //=> ["b", "c"]
a                                       //=> ["a"]

& operation

[ 1, 1, 3, 5 ].and([ 1, 2, 3 ])         //=> [ 1, 3 ]

*

[ 1, 2, 3 ].x(3)                        //=> [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
[ 1, 2, 3 ].x(",")                      //=> "1,2,3"

+

Ruby [ 1, 2, 3 ] + [ 4, 5 ]             //=> [ 1, 2, 3, 4, 5 ]
JS [ 1, 2, 3 ].plus([ 4, 5 ])           //=> [ 1, 2, 3, 4, 5 ]

-

Ruby [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ]       //=>  [ 3, 3, 5 ]
JS [ 1, 1, 2, 2, 3, 3, 4, 5 ].minus([ 1, 2, 4 ])    //=>  [ 3, 3, 5 ]

assoc

bs1 = [ "colors", "red", "blue", "green" ]
 s2 = [ "letters", "a", "b", "c" ]
 s3 = "foo"
 a  = [ s1, s2, s3 ]
 a.assoc("letters")                     //=> [ "letters", "a", "b", "c" ]
 a.assoc("foo")                         //=> nil

count

browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
browsers.length                         //=> 5
browsers.count                          //=> 5

rassoc

a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
a.rassoc("two")                         //=> [2, "two"]
a.rassoc("four")                        //=> nil

at

a = [ "a", "b", "c", "d", "e" ]
a.at(0)                                 //=> "a"
a.at(-1)                                //=> "e"

clear

a = [ "a", "b", "c", "d", "e" ]
a.clear                                 //=> [ ]

compact

[ "a", nil, "b", nil, "c", nil ].compact
                                        //=> [ "a", "b", "c" ]
js:
[1, 2, 3, null, undefined, true, false, 4, 5].compact() //=> [1, 2, 3, true, false, 4, 5]

cycle

a = ["a", "b", "c"]
a.cycle {|x| puts x }                   // print, a, b, c, a, b, c,.. forever.
a.cycle(2) {|x| puts x }                // print, a, b, c, a, b, c.

delete

a = [ "a", "b", "b", "b", "c" ]
a.delete("b")                           //=> "b"
a                                       //=> ["a", "c"]
a.delete("z")                           //=> nil
a.delete("z") { "not found" }           //=> "not found"

delete_at(index)

a = %w( ant bat cat dog )
a.delete_at(2)                          //=> "cat"
a                                       //=> ["ant", "bat", "dog"]
a.delete_at(99)                         //=> nil

keep_if [destr

Related Skills

View on GitHub
GitHub Stars12
CategoryDevelopment
Updated1mo ago
Forks1

Languages

JavaScript

Security Score

80/100

Audited on Feb 22, 2026

No findings