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"


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'ext/fast_underscore/fast_underscore.c', line 352

static VALUE
str_underscore(VALUE rb_string) {
  VALUE resultant;
  rb_encoding *encoding;

  char *string;
  char *end;

  builder_t *builder;
  codepoint_t *codepoint;

  encoding = rb_enc_from_index(ENCODING_GET(rb_string));
  string = RSTRING_PTR(rb_string);
  end = RSTRING_END(rb_string);

  builder = builder_build(RSTRING_LEN(rb_string) * 2);
  codepoint = codepoint_build(encoding);

  while (string < end) {
    codepoint->character = rb_enc_codepoint_len(string, end, &codepoint->size, encoding);
    builder_next(builder, codepoint);
    string += codepoint->size;
  }
  builder_flush(builder);

  resultant = rb_enc_str_new(builder->result, builder->result_size, encoding);
  builder_free(builder);
  codepoint_free(codepoint);

  return resultant;
}