Class: Symbol
- Includes:
- Comparable
- Defined in:
- src/symbol.c,
mrbgems/mruby-symbol-ext/mrblib/symbol.rb
Overview
15.2.11
Instance Method Summary collapse
-
#<=> ⇒ Object
15.2.11.3.5(x).
-
#==(obj) ⇒ Boolean
Equality—If sym and obj are exactly the same symbol, returns
true. -
#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.
-
#id2name ⇒ Object
Returns the name or string corresponding to sym.
-
#inspect ⇒ Object
15.2.11.3.5(x).
-
#to_proc ⇒ Object
-
#to_s ⇒ Object
Returns the name or string corresponding to sym.
-
#to_sym ⇒ Object
(also: #intern)
In general,
to_symreturns theSymbolcorresponding 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)
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'src/symbol.c', line 449 static mrb_value sym_cmp(mrb_state *mrb, mrb_value s1) { mrb_value s2; mrb_sym sym1, sym2; mrb_get_args(mrb, "o", &s2); if (mrb_type(s2) != MRB_TT_SYMBOL) 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; p1 = mrb_sym2name_len(mrb, sym1, &len1); p2 = mrb_sym2name_len(mrb, sym2, &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); } } |
#==(obj) ⇒ Boolean
Equality—If sym and obj are exactly the same
symbol, returns true.
222 223 224 225 226 227 228 229 230 |
# File 'src/symbol.c', line 222 static mrb_value sym_equal(mrb_state *mrb, mrb_value sym1) { mrb_value sym2; mrb_get_args(mrb, "o", &sym2); return mrb_bool_value(mrb_obj_equal(mrb, sym1, sym2)); } |
#capitalize ⇒ Object
call-seq: sym.capitalize -> symbol
Same as sym.to_s.capitalize.intern.
18 19 20 |
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 18 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#<=>.
48 49 50 51 52 53 |
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 48 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.
62 63 64 65 66 |
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 62 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.
28 29 30 |
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 28 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.
74 75 76 |
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 74 def empty? self.length == 0 end |
#id2name ⇒ String #to_s ⇒ String
Returns the name or string corresponding to sym.
:fred.id2name #=> “fred”
243 244 245 246 247 248 249 250 251 252 |
# File 'src/symbol.c', line 243 static mrb_value mrb_sym_to_s(mrb_state *mrb, mrb_value sym) { mrb_sym id = mrb_symbol(sym); const char *p; mrb_int len; p = mrb_sym2name_len(mrb, id, &len); return mrb_str_new_static(mrb, p, len); } |
#inspect ⇒ Object
15.2.11.3.5(x)
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'src/symbol.c', line 397 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_sym2name_len(mrb, id, &len); str = mrb_str_new(mrb, 0, len+1); sp = RSTRING_PTR(str); RSTRING_PTR(str)[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_dump(mrb, str); sp = RSTRING_PTR(str); sp[0] = ':'; sp[1] = '"'; } return str; } |
#to_proc ⇒ Object
6 7 8 9 10 |
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 6 def to_proc ->(obj,*args,&block) do obj.__send__(self, *args, &block) end end |
#id2name ⇒ String #to_s ⇒ String
Returns the name or string corresponding to sym.
:fred.id2name #=> “fred”
243 244 245 246 247 248 249 250 251 252 |
# File 'src/symbol.c', line 243 static mrb_value mrb_sym_to_s(mrb_state *mrb, mrb_value sym) { mrb_sym id = mrb_symbol(sym); const char *p; mrb_int len; p = mrb_sym2name_len(mrb, id, &len); return mrb_str_new_static(mrb, p, len); } |
#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.
265 266 267 268 269 |
# File 'src/symbol.c', line 265 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.
38 39 40 |
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 38 def upcase (self.to_s.upcase! || self).to_sym end |