These can be called through the str object on ruby:
ruby.str.downcase('hEllO'); // hello
Or, if you call the add_methods_to_string_prototype
method, you can call each applicable method as a property on any string.
ruby = require('ruby');
ruby.add_methods_to_string_prototype();
'hEllO'.downcase; // hello
The examples below will always use the prototype version, unless otherwise specified.
export default class _String {
constructor () {
this.chars = chars;
this.downcase = downcase;
this.empty = empty;
this.empty_questionmark = this.empty;
this.reverse = reverse;
this.upcase = upcase;
}
Creates a new string.
Only available on ruby.str
, not as a method on an instance of String
.
let new_string = ruby.str.new('abc');
new_string; // 'abc'
new (str) {
return `${str}`;
}
Returns the characters of the string in an array.
let chars = 'abc'.chars;
chars; // ['a', 'b', 'c']
function chars(str) {
str = str || this;
return str.split('');
}
function downcase(str) {
str = str || this;
return str.toLowerCase();
}
Returns true if the string is empty and false if it is not.
'hello'.empty; // false
' '.empty; // false
''.empty; // true
empty_questionmark
is an alias for empty
.
function empty(str) {
if (str === undefined) { // eslint-disable-line no-undefined
str = this;
}
return str.length === 0;
}
function reverse(str) {
str = str || this;
let parts = str.split('');
let reversedParts = parts.reverse();
let reversed = reversedParts.join('');
return reversed;
}