Class: String

Inherits:
Object
  • Object
show all
Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#underscoreObject

Makes an underscored, lowercase form from the expression in the string.

Changes ‘::’ to ‘/’ to convert namespaces to paths.

underscore('ActiveModel')         # => "active_model"
underscore('ActiveModel::Errors') # => "active_model/errors"

As a rule of thumb you can think of underscore as the inverse of #camelize, though there are cases where that does not hold:

camelize(underscore('SSLError'))  # => "SslError"


300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'ext/fast_underscore/fast_underscore.c', line 300

static VALUE underscore(VALUE string) {
  char segment[RSTRING_LEN(string) * 2 * sizeof(unsigned int) * 2];
  char result[RSTRING_LEN(string) * 2 * sizeof(unsigned int) * 2];

  builder_t builder = {
    .state = STATE_DEFAULT,
    .segment = segment,
    .result = result,
    .segment_size = 0,
    .result_size = 0,
    .push_next = 0
  };

  codepoint_t codepoint = {
    .encoding = rb_enc_from_index(ENCODING_GET(string)),
    .character = 0,
    .size = 0
  };

  char *pointer = RSTRING_PTR(string);
  char *end = RSTRING_END(string);

  while (pointer < end) {
    codepoint.character = rb_enc_codepoint_len(pointer, end, &codepoint.size, codepoint.encoding);
    builder_next(&builder, &codepoint);
    pointer += codepoint.size;
  }

  builder_flush(&builder);
  return rb_enc_str_new(builder.result, builder.result_size, codepoint.encoding);
}