Rubymonkey
Monkey patching Javascript to be more like Ruby
Install / Use
/learn @daz-codes/RubymonkeyREADME
Ruby Monkey
Unashamedly monkey patching JavaScript to be more like Ruby.
<img width="250" height="250" alt="ruby monkey" src="https://github.com/user-attachments/assets/cee09e69-6617-415b-ab66-7880032c3b63" />Ruby (and Rails) has loads of really nice methods, now you can use them in JS as well!
Ruby Monkey helps to make your JavaScript code more code elegant, fun and productive!
Now you can write JS code like this:
[1,2,3].last // 3
[1,2,3].count // 3
(21).ordinalize // "21st"
"RubyMonkey".downcase.reverse // "yeknomybur"
[1,2,3].sum.squared // 36
["A","A","C","A","B","A","B"].tally // {"A": 4, "C": 1, "B": 2}
(1).day.ago // yesterday
Usage
npm install rubymonkey
Then just add either require "rubymonkey" or import "rubymonkey" to the top of any JS file and suddenly coding in JS becomes a lot more fun and productive!
In general, if you know the Ruby methods you should be able to use them in almost the same way, with a few slight changes:
- Blocks change to arrow functions
- JavaScript does not support appending symbols to the end of function names, so Boolean methods can't end in a
?, so these have 2 versions, one without the?at the end and another withisprepended to the beginning.
So for example, this Ruby:
[1,2,3].count{ |n| n.odd? }
Would be written in Ruby Monkey as either of the following:
[1,2,3].count( n => n.isOdd )
[1,2,3].count( n => n.odd )
Template to Func
Ruby has this really nice syntax to make calling methods on objects easier, so instead of [1,2,3].map { |n| n.next } you can just write [1,2,3].map(&:next)
JavaScript doesn't let you use & and doesn't have symbol literals, but you can use $ and it does have template literals and tag functions, so in Ruby Doo, you can do the same thing like this:
[1,2,3].map($`next`)
Number Methods
number.even & number.isEven
Checks if the number is even.
(4).even; // true
(5).even; // false
number.odd & number.isOdd
Checks if the number is odd.
(3).odd; // true
(10).odd; // false
number.to_s
Converts the number to a string.
(42).to_s; // "42"
number.next
Returns the next integer.
(10).next; // 11
number.round
Rounds the number to the nearest integer.
(4.7).round; // 5
(4.2).round; // 4
number.ceil
Returns the smallest integer greater than or equal to the number.
(4.2).ceil; // 5
number.floor
Returns the largest integer less than or equal to the number.
(4.9).floor; // 4
number.digits
Returns an array of the digits of the number.
(123).digits; // [1, 2, 3]
number.factors
Returns an array of all factors of the number.
(12).factors; // [1, 2, 3, 4, 6, 12]
number.prime
Checks if the number is prime.
(7).prime; // true
(9).prime; // false
number.integer and number.isInteger
Checks if the number is an integer.
(10.5).integer; // false
(10).integer; // true
number.positive & number.isPositive
Checks if the number is positive.
(5).positive; // true
(-3).positive; // false
number.negative & number.isNegative
Checks if the number is negative.
(-10).negative; // true
(5).negative; // false
number.zero & number.isZero
Checks if the number is zero.
(0).zero; // true
(1).zero; // false
number.squared
Returns the square of the number.
(4).squared; // 16
number.cubed
Returns the cube of the number.
(3).cubed; // 27
number.ordinal
Returns the ordinal suffix of the number.
(1).ordinal; // "st"
(2).ordinal; // "nd"
(3).ordinal; // "rd"
(4).ordinal; // "th"
(11).ordinal; // "th"
number.ordinalize
Returns the number as an ordinal string.
(1).ordinalize; // "1st"
(2).ordinalize; // "2nd"
(3).ordinalize; // "3rd"
(4).ordinalize; // "4th"
(11).ordinalize; // "11th"
number.upto(n, func?)
Iterates from the current number up to n, calling func if provided.
(3).upto(6, console.log);
// Logs: 3, 4, 5, 6
(3).upto(6);
// Returns: [3, 4, 5, 6]
number.times(func)
Executes func the given number of times, passing the index as an optional argument.
(3).times(_ => console.log("Ruby!"));
// Logs: Ruby!Ruby!Ruby!
(3).times(i => console.log(`Iteration: ${i}`));
// Logs: Iteration: 0, Iteration: 1, Iteration: 2
number.mod(n)
Returns the remainder of the number divided by n.
(10).mod(3); // 1
number.divmod(n)
Returns an array containing the quotient and remainder of division by n.
(10).divmod(3); // [3, 1]
number.gcd(n)
Computes the greatest common divisor (GCD) of the number and n.
(48).gcd(18); // 6
number.lcm(n)
Computes the least common multiple (LCM) of the number and n.
(4).lcm(6); // 12
number.between(a, b) & number.isBetween(a,b)
Checks if the number is between a and b (inclusive).
(5).between(1, 10); // true
(15).between(1, 10); // false
number.eql(n)
Checks if the number is strictly equal to n.
(5).eql(5); // true
(5).eql(3); // false
number.multiple_of(n) & number.divisble_by(n)
Checks if the number is a multiple of n.
(10).multiple_of(5); // true
(10).multiple_of(3); // false
(10).divisible_by(5); // true
(10).divisible_by(3); // false
number.divisor_of(n) & number.factor_of(n)
Checks if the number is a divisor (factor) of n.
(5).divisor_of(10); // true
(3).divisor_of(10); // false
(5).factor_of(10); // true
(3).factor_of(10); // false
number.pred
Returns the predecessor (this - 1).
(10).pred; // 9
(1).pred; // 0
number.downto(n, func?)
Iterates from the current number down to n, calling func if provided.
(6).downto(3, console.log);
// Logs: 6, 5, 4, 3
(6).downto(3);
// Returns: [6, 5, 4, 3]
number.div(n)
Returns the integer division result (floor of division).
(10).div(3); // 3
(7).div(2); // 3
number.modulo(n)
Returns the modulo of the number divided by n. Alias for mod.
(10).modulo(3); // 1
(-10).modulo(3); // 2
number.hcf(n)
Highest common factor. Alias for gcd.
(48).hcf(18); // 6
number.nonzero & number.isNonzero
isNonzero returns true if the number is not zero. nonzero returns the number itself if non-zero, otherwise undefined.
(5).isNonzero; // true
(0).isNonzero; // false
(5).nonzero; // 5
(0).nonzero; // undefined
number.abs
Returns the absolute value of the number.
(-5).abs; // 5
(5).abs; // 5
number.add(n)
Returns the sum of the number and n.
(5).add(3); // 8
number.subtract(n) & number.minus(n)
Returns the difference of the number and n.
(10).subtract(3); // 7
(10).minus(3); // 7
number.multiply(n)
Returns the product of the number and n.
(5).multiply(3); // 15
number.divide(n)
Returns the quotient of the number divided by n.
(10).divide(2); // 5
(7).divide(2); // 3.5
String Methods
string.reverse
Returns the string reversed.
"hello".reverse; // "olleh"
string.size
Returns the length of the string.
"hello".size; // 5
string.to_i
Converts the string to an integer, returning 0 if conversion fails.
"123".to_i; // 123
"abc".to_i; // 0
string.to_f
Converts the string to a float, returning 0 if conversion fails.
"12.34".to_f; // 12.34
"abc".to_f; // 0
string.downcase
Returns the string in lowercase.
"Hello".downcase; // "hello"
string.upcase
Returns the string in uppercase.
"hello".upcase; // "HELLO"
string.upcase_first
Capitalizes only the first character of the string.
"hello world".upcase_first; // "Hello world"
string.downcase_first
Lowercases only the first character of the string.
"Hello World".downcase_first; // "hello World"
string.squish
Removes leading, trailing, and multiple consecutive spaces.
" Hello world ".squish; // "Hello world"
string.blank
Checks if the string is empty or contains only whitespace.
" ".blank; // true
"hello".blank; // false
string.empty
Checks if the string is completely empty (not even whitespace).
"".empty; // true
" ".empty; // false
string.humanize
Removes _id from the end (if present) and replaces underscores with spaces, capitalizing the first letter.
"user_name".humanize; // "User name"
"post_id".humanize; // "Post"
string.titleize (Alias: titlecase)
Capitalizes each word in the string.
"hello world".titleize; // "Hello World"
string.parameterize
Converts the string into a URL-friendly format (lowercase, hyphenated).
"Hello, World!".parameterize; // "hello-world"
string.chars
Returns an array of individual characters.
"hello".chars; // ["h", "e", "l", "l", "o"]
string.count(substring)
Returns the number of times substring appears in the string.
"hello world".count("l"); // 3
string.starts_with(substring)
Checks if the string starts with the given substring.
"hello world".starts_with("hello"); //
