How to convert JavaScript to Ruby?

I found this pretty old blog post about converting JavaScript code to Ruby code . However, the gems used in the examples (both rkelly and ruby2ruby ) seem to have changed a lot over the past 7 years, and now these examples do not work with them. I tried to play with gems a bit, but I am not a Ruby expert, and I have no experience in analyzing and processing languages, so my attempts were not successful.

My question is: is there another way to convert JavaScript code to Ruby code with these gems? And if so, how?

+4
source share
1 answer

, - . , . , , Google Translate.

- JavaScript, Ruby, JavaScript Ruby .

JavaScript, , Ruby. , , .

, JavaScript:

var count = 0;
_.each(array, function(e) {
  count += Math.pow(2, e);
})

return count;

Ruby:

array.inject(0) do |s, e|
  s + 2 ** e
end

, API .

+1

Source: https://habr.com/ru/post/1543441/


All Articles