Functions
The Functions filter provides a large number of convenience methods Rubyists are familar with. Statements such as "252.3".to_i transform into parseInt("252.3"), or [1,3,5].yield_self { |arr| arr[1] } into (arr => arr[1])([1, 3, 5]). Generally you will want to include this filter in your configuration unless you have specific reason not to.
List of Transformations
.absMath.abs().all?.every.any?.some.ceilMath.ceil().charsArray.from().chrfromCharCode.clear.length = 0.define_methodklass.prototype.meth = function ....deletedelete target[arg].downcase.toLowerCase.each.forEach.each_keyfor (i in ...) {}.each_pairfor (var key in item) {var value = item[key]; ...}.each_value.forEach.each_with_index.forEach.end_with?.slice(-arg.length) == arg.empty?.length == 0.find_indexfindIndex.first[0].first(n).slice(0, n).floorMath.floor().gsubreplace(//g).include?.indexOf() != -1.indexindexOf(when using arg) orfindIndex(when using block).inspectJSON.stringify().keys()Object.keys().last[*.length-1].last(n).slice(*.length-1, *.length).lstrip.replace(/^\s+/, "").maxMath.max.apply(Math).mergeObject.assign({}, ...).merge!Object.assign().method_defined?klass.prototype.hasOwnProperty(meth)ormeth in klass.prototype.minMath.min.apply(Math).new(size,default)== .new(size).fill(default).nil?== null.ordcharCodeAt(0)putsconsole.lograndMath.random.replace.length = 0; ...push.apply(*).respond_to?right in left.rindex.lastIndexOf.roundMath.round().rstrip.replace(/s+$/, "").scan.match(//g).sum.reduce(function(a, b) {a + b}, 0).start_with?.substring(0, arg.length) == argor.startsWith(arg)for ES2015+.upto(lim)for (var i=num; i<=lim; i+=1).downto(lim)for (var i=num; i>=lim; i-=1).step(lim, n).eachfor (var i=num; i<=lim; i+=n).step(lim, -n).eachfor (var i=num; i>=lim; i-=n)(0..a).to_aArray.apply(null, {length: a}).map(Function.call, Number)(b..a).to_aArray.apply(null, {length: (a-b+1)}).map(Function.call, Number).map(function (idx) { return idx+b })(b...a).to_aArray.apply(null, {length: (a-b)}).map(Function.call, Number).map(function (idx) { return idx+b }).strip.trim.sub.replace.tap {|n| n}(function(n) {n; return n})(...).to_fparseFloat.to_iparseInt.to_s.toString.to_jsonJSON.stringify(obj).upcase.toUpperCase.yield_self {|n| n}(function(n) {return n})(...)[-n][*.length-n]for literal values ofn[n...m].slice(n,m)[n..m].slice(n,m+1)[/r/, n].match(/r/)[n][/r/, n]=.replace(/r/, ...)(1..2).each {|i| ...}for (var i=1 i<=2; i+=1)"string" * lengthnew Array(length + 1).join("string")or"string".repeat(length)for ES2015+@foo.call(args)this._foo(args)@@foo.call(args)this.constructor._foo(args)Array(x)Array.prototype.slice.call(x)delete xdelete x(note lack of parenthesis)
Additional Features
.sub!and.gsub!become equivalentx = x.replacestatements.map!,.reverse!, and.select!become equivalent.splice(0, .length, *.method())statementssetIntervalandsetTimeoutallow block to be treated as the first parameter on the call- for the following methods, if the block consists entirely of a simple
expression (or ends with one), a
returnis added prior to the expression:sub,gsub,any?,all?,map,find,find_index. - New classes subclassed off of
Exceptionwill become subclassed off ofErrorinstead; and default constructors will be provided loop do...endwill be replaced withwhile (true) {...}raise Exception.new(...)will be replaced withthrow new Error(...)block_given?will check for the presence of optional argument_implicitBlockYieldwhich is a function made accessible through the use ofyieldin a method body.alias_methodworks both inside of a class definition as well as called directly on a class name (e.g.MyClass.alias_method)
Additionally, there is two mappings that will only be done if explicitly
included (pass include: [:class, :call] as a convert option to enable):
.class.constructora.calla()