Enjoy Ruby like functions in jS Objects; make your life easier
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># Gemfile
gem 'rubyinjs'// in application.js
//= require 'rubyinjs'"Ho! ".x(3) //=> "Ho! Ho! Ho! ""hello".capitalize() //=> "Hello""abcdef".casecmp_("abcde") //=> false
"aBcDeF".casecmp_("abcdef") //=> true
"abcdef".casecmp_("abcdefg") //=> false
"abcdef".casecmp_("ABCDEF") //=> true"hello".center(4) //=> "hello"
"hello".center(20) //=> " hello "
"hello".center(20, '123') //=> "1231231hello12312312""abcde".chr() //=> "a""hEllO".downcase() //=> "hello""hEllO".upcase() //=> "HELLO""hello".each_char(function(c){ console.log(c, ' ') })
produces:
h e l l o"hello".empty_ //=> false
" ".empty _ //=> false
"".empty_ //=> true"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"hello".gsub(/[aeiou]/, '*') //=> "h*ll*"
"hello".gsub(/([aeiou])/, '<\1>') //=> "h<e>ll<o>""hello".include_("lo") //=> true
"hello".include_("ol") //=> false
"hello".include_('h') //=> true"hello".index('e') //=> 1
"hello".index('lo') //=> 3
"hello".index('a') //=> nil
"hello".index('e') //=> 1"hello\nworld\n".lines() //=> ["hello\n", "world\n"]
"hello world".lines(' ') //=> ["hello ", " ", "world"]
"hello\nworld\n".lines() //=> ["hello", "world"]" hello ".lstrip() //=> "hello "
"hello".lstrip() //=> "hello"" hello ".rstrip() //=> " hello"
"hello".rstrip() //=> "hello""Ruby".match_(/R.../) //=> true
"Ruby".match_(/R.../, 1) //=> false
"Ruby".match_(/P.../) //=> false"abcd".succ() //=> "abce"
"THX1138".succ() //=> "THX1139"
"<<koala>>".succ()"abcd".next() //=> "abce"
"THX1138".next() //=> "THX1139"
"<<koala>>".next() //=> "<<koalb>>""a".ord() //=> 49// Non mutative
a = "!"
a.prepend("hello ", "world") //=> "hello world!"
a.prepend("hello ", "world", 'shiva') //=> "hello world shiva!""stressed".reverse() //=> "desserts"a = "cruel world"
a.scan(/\w+/) //=> ["cruel", "world"]
a.scan(/.../) //=> ["cru", "el ", "wor"]
a.scan(/(...)/) //=> [["cru"], ["el "], ["wor"]]
a.scan(/(..)(..)/) //=> [["cr", "ue"], ["l ", "wo"]]"cruel world".size() //=> 11"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"hello".sub(/[aeiou]/, '*') //=> "h*llo""Hello".swapcase() //=> "hELLO"
"cYbEr_PuNk11".swapcase() //=> "CyBeR_pUnK11""123.45e1".to_f() //=> 1234.5
"45.67 degrees".to_f() //=> 45.67
"thx1138".to_f() //=> 0.0"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[1, 2, 3, 4, 5, 6].at(1) //=> 2arr = ['a', 'b', 'c', 'd', 'e', 'f']
arr.fetch(100) //=> IndexError: index 100 outside of array bounds: -6...6
arr.fetch(100, "oops") //=> "oops"
arr = ['a', 'b', 'c', 'd', 'e', 'f']
arr.take(1) //=> ['a']
arr.take(2) //=> ['a', 'b']arr = ['a', 'b', 'c', 'd', 'e', 'f']
arr.first() //=> 'a'
arr.first(2) //=> ['a', 'b']arr = ['a', 'b', 'c', 'd', 'e', 'f']
arr.last //=> 'f'
arr.last(2) //=> ['e', 'f']browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
browsers.length //=> 5
browsers.count //=> 5browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
browsers.include_('Konqueror') //=> falsebrowsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
browsers.empty_ //=> falsea = %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"][ 1, 2, 3, 4, 5 ].size() //=> 5 1, 2, 3, 4, 5 ].collect {} //=> 5a = [ "a", "b", "c", "d" ]
a.pop //=> "d"
a.pop(2) //=> ["b", "c"]
a //=> ["a"][ 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 ]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") //=> nilbrowsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
browsers.length //=> 5
browsers.count //=> 5a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
a.rassoc("two") //=> [2, "two"]
a.rassoc("four") //=> nila = [ "a", "b", "c", "d", "e" ]
a.at(0) //=> "a"
a.at(-1) //=> "e"a = [ "a", "b", "c", "d", "e" ]
a.clear //=> [ ][ "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]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.a = [ "a", "b", "b", "b", "c" ]
a.delete("b") //=> "b"
a //=> ["a", "c"]
a.delete("z") //=> nil
a.delete("z") { "not found" } //=> "not found"a = %w( ant bat cat dog )
a.delete_at(2) //=> "cat"
a //=> ["ant", "bat", "dog"]
a.delete_at(99) //=> nila = [ "a", "b", "c", "d", "e"]
a.keep_if((v)=>v.match(/[aeiou]/)) //=> ["a", "e"]a = [ "a", "b", "c", "d", "e"]
a.reject((v)=>v.match(/[aeiou]/)) //=> ["b", "c", "d"]a = [ "a", "b", "c", "d", "e"]
a.select((v)=>v.match(/[aeiou]/)) //=> ["a", "e"]a = [ "a", "b", "c" ]
a.reverse_each {|x| print x, " " }
produces:
c b aa = [ "a", "b", "c" ]
a.sample() //=> 'a'
a.sample() //=> 'b'
a.sample() //=> 'a'
a.sample(2) //=> ['a', 'c']a = [[1,2], [3,4], [5,6]]
a.transpose() //=> [[1, 3, 5], [2, 4, 6]]a = [ "a", "a", "b", "b", "c" ]
a.uniq() //=> ["a", "b", "c"]a = [ "a", "a", "b", "b", "c" ]
a.each((x)=>console.log(x))
Output:
a
a
b
b
cYou need to have a working Ruby language installed in your system.
$ which ruby
/Users/john/.rvm/rubies/ruby-2.6.3/bin/rubybrew install ruby$ apt-get install rubyOr install ruby using RVM or rbenv
You can run a development server using following command
$ bundle install
# Symlink the lib dir to examples/lib, otherwise, server wont be able to serve js file
$ ln -s $(pwd)/lib/ $(pwd)/examples/
$ rackup -p 3000
Puma starting in single mode...
* Version 4.0.1 (ruby 2.6.3-p62), codename: 4 Fast 4 Furious
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
To contribute to the project, fork the repo and clone it locally.
Then start a rack server using following command
$ bundle install
$ rackup -p 9292This will start a server at http://localhost:9292
Now in console you can test the methods you modify in ruby.js file
We use a rake task to build the site. To build the site, run following command
$ bundle exec rakeThe site is deployed using GitHub pages. To deploy the site just push the changes to master branch
and GitHub action will take care of the rest.
See the workflow file .github/workflows/site-builder.yml
After deployment the latest version of the ruby.js library will be available at
https://the-ruby-js-project.github.io/rubyinjs/ruby.js
Shiva Bhusal - GitHub
