Do you have a JavaScript library that provides Ruby-like APIs?

Asked 1 years ago, Updated 1 years ago, 40 views

I'm usually used to Ruby, so when I use JavaScript to manipulate arrays, hashes, strings, etc., it's troublesome to look for or implement Ruby's method in JavaScript.

So please let me know if there is a JS library that can manipulate basic objects like Ruby.

javascript ruby

2022-09-30 18:13

5 Answers

As Tetsuwo OISHI wrote, there is a library called RubyJS. The current version is a new version called 0.8.0-beta1.It may not be well known yet, but it may be good for those who like Ruby-like coding.

RubyJShttp://rubyjs.org/

According to the document, there are two ways to use it.

  • RubyJS(Lite)-Functional
  • RubyJS Classic-Object Oriented

First of all, RubyJS(Lite) seems to be implemented in a functional style similar to Underscore.js.

String-related functions can be found in the global object '_s'
  Array-related functions can be found in the global object '_a'
  Time-related functions can be found in the global object '_t'...

That's how it goes.

On the other hand, RubyJS Classic seems to implement the Lite version of the feature using a wrapper object named 'R'.This function returns the wrapper object itself as a return value, allowing you to form a method chain.

R("foo");//=>{R.String:"foo"}
  R("foo").capitalize();//=>{R.String:"Foo"}
  R("foo").capitalize().ljust(7, '-');//=>{R.String:"--Foo--"}

RubyJS' Document Page contains code comparing pure JS to Underscore.js.Choose two of them and supplement them below.

//JS
 ['10', '10', '10'].map(parseInt)//=> [10, NaN, 2]
 // undercore
 _.map(['10', '10', '10', parseInt)//=>[10, NaN, 2]
 // RubyJS
 _a.map(['10', '10', '10', parseInt)//=>[10, 10, 10]

I think what this code is trying to say is, "JS and underscore can't parseInt all the strings in the array, but RubyJS is easy."That's true, but if things go on like this, I feel a little sorry for JS and underscore, so I'll try to supplement them below.

For pure Javascript

When parseInt using the Array.map() in Javascript, writing as follows will result in unexpected results.

['10', '10', '10'].map(parseInt);//=>[10, NaN, 2]

This is due to more than one argument for the parseInt() function.

var intValue=parseInt(string[,radix]); // The second argument is a cardinal (10 for decimal)

Quoted from Mozilla Developer Network

There are several workarounds, one using the bind() function, where you can specify a base of 10 (decimal) for the second argument of bind().

['10', '10', '10'].map(parseInt.bind(null,10));//=>[10,10,10]

Alternatively, you can use Number() instead of parseInt().The Number() in JavaScript is a wrapper object, but it acts as a type conversion function when called without the new operator.

['10', '10', '10'].map(Number);//=>[10, 10, 10]

This is simple, and I don't think it's inferior to RubyJS.

For Underscore.js

By default, Underscore defines a global object named _ (underscore).Associate the callback function to an array using the map function implemented in the _.As with the JS mentioned above, you can quantify it in one of the following ways:

_.map(['10', '10', '10', parseInt.bind(null,10));//=>[10, 10, 10]
 _.map(['10', '10', '10', Number);//=>[10, 10, 10]

For RubyJS

RubyJS defines array-related methods for global objects named _a. You can write either of the following:

_a.map(['10', '10', '10', parseInt);//=>[10, 10, 10]
 _a.map(['10', '10', '10', Number);//=>[10, 10, 10]

It's certainly simple to code.

Let me look at another comparison.This section contains the sort() code for the array.

//JS
[1,8,15].sort()//=> [1,15,8]
// undercore
_.sortBy([1,8,15])//=>[1,15,8]
// RubyJS
_a.sort([1,8,15])//=>[1,8,15]

I think what this code is trying to say is, "JS and underscore don't simply sort the strings in the array, but RubyJS is easy."In order to make it easier to understand, I will rewrite the numbers and think as follows.

//JS
 [33,4,1111,222].sort()//=> [1111,222,33,4]
 // undercore
 _.sortBy([33,4,1111,222]) // =>[1111,222,33,4]
 // RubyJS
 _a.sort([33,4,1111,222]) // =>[4,33,222,1111]

For pure Javascript

Using the Array.sort() in Javascript, you can get the results sorted alphabetically.

[33,4,1111,222].sort();//=>[1111,222,33,4]

If you want to compare numbers instead of alphabets, you can write them as follows:

[33,4,1111,222].sort(function(a,b){return a-b;});//=>[4,33,222,1111]

For Underscore.js

Undercore can use sortBy() to:

_.sortBy([33,4,1111,222], function(num) {return num;});//=>[4,33,222,1111]

For RubyJS

_a.sort([33,4,1111,222])//=>[4,33,222,1111]

It's certainly simple.

That's all.It may be good for those who like Ruby-like coding.

We have placed the above code in JSFiddle.


2022-09-30 18:13

There is a site/library called Opal that converts Ruby code to Javascript.

http://opalrb.org

The main Ruby function is in the library, so I think I can convert it and check it.


2022-09-30 18:13

If the Ruby-like API refers to module Enumerable, I think Undercore or its string extension library is the major one.If you search the functional programming library for javascript, it will be displayed at the top.

var stooges=[{name:'curry', age:25}, {name:'moe', age:21}, {name:'larry', age:23}];
var youngest=_.chain(stooges)
  .sortBy(function(stooge){returnstooge.age;})
  .map(function(stooge){return stooge.name+'is'+stooge.age;})
  .first()
  .value();
=>"moe is 21"

Integrated with _.mixin(_.str.exports()); // underscore
_('capitalize dash-CamelCase_undercore trim').humanize()
=>'Capitalize dash camel case undercore trim'
['foo20', 'foo5'].sort(_.naturalCmp)
=>['foo5', 'foo20']

fn.js can also be curied.


2022-09-30 18:13

There is a JS library that mimics Ruby's API called RubyJS.

It seems that it should be wrapped in R as shown below.

R(1.2345).round(2)+1
// = > 2.23

Note: http://rubyjs.org/blog/2012/12/rubyjs-the-post-launch-launch-post/


2022-09-30 18:13

Sugar.js, which extends native objects like Ruby, was useful if you wanted to allow prototype extensions.Since the author is a rubyist, most ruby methods are defined.For more information, refer to the following documents:

Sugar:A Javascript library for working with native objects.

If you don't allow prototype extensions, you'll probably use underscore.js or lodash.js.


2022-09-30 18:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.