Class: Symbol
- Includes:
- Comparable
- Defined in:
- mruby/src/symbol.c,
mruby/mrblib/symbol.rb,
mruby/mrbgems/mruby-symbol-ext/mrblib/symbol.rb
Overview
15.2.11
Instance Method Summary collapse
-
#<=> ⇒ Object
15.2.11.3.5(x).
-
#capitalize ⇒ Object
call-seq: sym.capitalize -> symbol.
-
#casecmp(other) ⇒ Object
call-seq: sym.casecmp(other) -> -1, 0, +1 or nil.
-
#casecmp?(sym) ⇒ Boolean
call-seq: sym.casecmp?(other) -> true, false, or nil.
-
#downcase ⇒ Object
call-seq: sym.downcase -> symbol.
-
#empty? ⇒ Boolean
call-seq: sym.empty? -> true or false.
-
#inspect ⇒ Object
15.2.11.3.5(x).
-
#name ⇒ String
Returns the name or string corresponding to sym.
- #to_proc ⇒ Object
-
#to_s ⇒ String
Returns the name or string corresponding to sym.
-
#to_sym ⇒ Object
(also: #intern)
In general,
to_sym
returns theSymbol
corresponding to an object. -
#upcase ⇒ Object
call-seq: sym.upcase -> symbol.
Methods included from Comparable
#<, #<=, #==, #>, #>=, #between?, #clamp
Instance Method Details
#<=> ⇒ Object
15.2.11.3.5(x)
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
# File 'mruby/src/symbol.c', line 660
static mrb_value
sym_cmp(mrb_state *mrb, mrb_value s1)
{
mrb_value s2 = mrb_get_arg1(mrb);
mrb_sym sym1, sym2;
if (!mrb_symbol_p(s2)) return mrb_nil_value();
sym1 = mrb_symbol(s1);
sym2 = mrb_symbol(s2);
if (sym1 == sym2) return mrb_fixnum_value(0);
else {
const char *p1, *p2;
int retval;
mrb_int len, len1, len2;
char buf1[8], buf2[8];
p1 = sym2name_len(mrb, sym1, buf1, &len1);
p2 = sym2name_len(mrb, sym2, buf2, &len2);
len = lesser(len1, len2);
retval = memcmp(p1, p2, len);
if (retval == 0) {
if (len1 == len2) return mrb_fixnum_value(0);
if (len1 > len2) return mrb_fixnum_value(1);
return mrb_fixnum_value(-1);
}
if (retval > 0) return mrb_fixnum_value(1);
return mrb_fixnum_value(-1);
}
}
|
#capitalize ⇒ Object
call-seq:
sym.capitalize -> symbol
Same as sym.to_s.capitalize.intern
.
12 13 14 |
# File 'mruby/mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 12 def capitalize (self.to_s.capitalize! || self).to_sym end |
#casecmp(other) ⇒ Object
call-seq:
sym.casecmp(other) -> -1, 0, +1 or nil
Case-insensitive version of Symbol#<=>
.
42 43 44 45 46 47 |
# File 'mruby/mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 42 def casecmp(other) return nil unless other.kind_of?(Symbol) lhs = self.to_s; lhs.upcase! rhs = other.to_s.upcase lhs <=> rhs end |
#casecmp?(sym) ⇒ Boolean
call-seq:
sym.casecmp?(other) -> true, false, or nil
Returns true if sym and other_sym are equal after case folding, false if they are not equal, and nil if other_sym is not a string.
56 57 58 59 60 |
# File 'mruby/mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 56 def casecmp?(sym) c = self.casecmp(sym) return nil if c.nil? return c == 0 end |
#downcase ⇒ Object
call-seq:
sym.downcase -> symbol
Same as sym.to_s.downcase.intern
.
22 23 24 |
# File 'mruby/mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 22 def downcase (self.to_s.downcase! || self).to_sym end |
#empty? ⇒ Boolean
call-seq:
sym.empty? -> true or false
Returns that sym is :“” or not.
68 69 70 |
# File 'mruby/mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 68 def empty? self.length == 0 end |
#inspect ⇒ Object
15.2.11.3.5(x)
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 |
# File 'mruby/src/symbol.c', line 587
static mrb_value
sym_inspect(mrb_state *mrb, mrb_value sym)
{
mrb_value str;
const char *name;
mrb_int len;
mrb_sym id = mrb_symbol(sym);
char *sp;
name = mrb_sym_name_len(mrb, id, &len);
str = mrb_str_new(mrb, NULL, len+1);
sp = RSTRING_PTR(str);
sp[0] = ':';
memcpy(sp+1, name, len);
mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX);
if (!symname_p(name) || strlen(name) != (size_t)len) {
str = mrb_str_inspect(mrb, str);
sp = RSTRING_PTR(str);
sp[0] = ':';
sp[1] = '"';
}
#ifdef MRB_UTF8_STRING
if (SYMBOL_INLINE_P(id)) RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
#endif
return str;
}
|
#name ⇒ String
Returns the name or string corresponding to sym. Unlike #to_s, the returned string is frozen.
:fred.name #=> "fred"
:fred.name.frozen? #=> true
430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'mruby/src/symbol.c', line 430
static mrb_value
sym_name(mrb_state *mrb, mrb_value vsym)
{
mrb_sym sym = mrb_symbol(vsym);
mrb_int len;
const char *name = mrb_sym_name_len(mrb, sym, &len);
mrb_assert(name != NULL);
if (SYMBOL_INLINE_P(sym)) {
return mrb_str_new_frozen(mrb, name, len);
}
return mrb_str_new_static_frozen(mrb, name, len);
}
|
#to_proc ⇒ Object
2 3 4 5 6 |
# File 'mruby/mrblib/symbol.rb', line 2 def to_proc ->(obj,*args,**opts,&block) do obj.__send__(self, *args, **opts, &block) end end |
#to_s ⇒ String
Returns the name or string corresponding to sym.
:fred.to_s #=> "fred"
414 415 416 417 418 |
# File 'mruby/src/symbol.c', line 414
static mrb_value
sym_to_s(mrb_state *mrb, mrb_value sym)
{
return mrb_sym_str(mrb, mrb_symbol(sym));
}
|
#to_sym ⇒ Object #intern ⇒ Object Also known as: intern
In general, to_sym
returns the Symbol
corresponding to an object. As sym is already a symbol, self
is returned in this case.
455 456 457 458 459 |
# File 'mruby/src/symbol.c', line 455
static mrb_value
sym_to_sym(mrb_state *mrb, mrb_value sym)
{
return sym;
}
|
#upcase ⇒ Object
call-seq:
sym.upcase -> symbol
Same as sym.to_s.upcase.intern
.
32 33 34 |
# File 'mruby/mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 32 def upcase (self.to_s.upcase! || self).to_sym end |