Class: Symbol

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
src/symbol.c,
mrblib/symbol.rb,
mrbgems/mruby-symbol-ext/mrblib/symbol.rb

Overview

15.2.11

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #==, #>, #>=, #between?, #clamp

Instance Method Details

#<=>Object

15.2.11.3.5(x)



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'src/symbol.c', line 566

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_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);
  }
}

#capitalizeObject

call-seq: sym.capitalize -> symbol

Same as sym.to_s.capitalize.intern.



12
13
14
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 12

def capitalize
  self.to_s.capitalize.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 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 42

def casecmp(other)
  return nil unless other.kind_of?(Symbol)
  lhs =  self.to_s.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.

Returns:

  • (Boolean)


56
57
58
59
60
# File '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

#downcaseObject

call-seq: sym.downcase -> symbol

Same as sym.to_s.downcase.intern.



22
23
24
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 22

def downcase
  self.to_s.downcase.to_sym
end

#empty?Boolean

call-seq: sym.empty? -> true or false

Returns that sym is :”” or not.

Returns:

  • (Boolean)


68
69
70
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 68

def empty?
  self.length == 0
end

#id2nameString #to_sString

Returns the name or string corresponding to sym.

:fred.id2name #=> “fred”

Overloads:



339
340
341
342
343
# File 'src/symbol.c', line 339

static mrb_value
sym_to_s(mrb_state *mrb, mrb_value sym)
{
  return mrb_sym_str(mrb, mrb_symbol(sym));
}

#inspectObject

15.2.11.3.5(x)



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'src/symbol.c', line 488

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, 0, 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;
}

#to_procObject



2
3
4
5
6
# File 'mrblib/symbol.rb', line 2

def to_proc
  ->(obj,*args,&block) do
    obj.__send__(self, *args, &block)
  end
end

#id2nameString #to_sString

Returns the name or string corresponding to sym.

:fred.id2name #=> “fred”

Overloads:



339
340
341
342
343
# File 'src/symbol.c', line 339

static mrb_value
sym_to_s(mrb_state *mrb, mrb_value sym)
{
  return mrb_sym_str(mrb, mrb_symbol(sym));
}

#to_symObject #internObject 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.



356
357
358
359
360
# File 'src/symbol.c', line 356

static mrb_value
sym_to_sym(mrb_state *mrb, mrb_value sym)
{
  return sym;
}

#upcaseObject

call-seq: sym.upcase -> symbol

Same as sym.to_s.upcase.intern.



32
33
34
# File 'mrbgems/mruby-symbol-ext/mrblib/symbol.rb', line 32

def upcase
  self.to_s.upcase.to_sym
end