Class: Symbol

Inherits:
Object show all
Defined in:
lib/core/facets/object/dup.rb,
lib/core/facets/symbol/not.rb,
lib/core/facets/symbol/as_s.rb,
lib/core/facets/symbol/call.rb,
lib/core/facets/symbol/succ.rb,
lib/core/facets/symbol/chomp.rb,
lib/core/facets/symbol/plain.rb,
lib/core/facets/symbol/op_div.rb,
lib/core/facets/symbol/thrown.rb,
lib/core/facets/symbol/generate.rb,
lib/core/facets/symbol/variablize.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate(key = nil) ⇒ Object

Generate a unique symbol.

Symbol.generate  #=> :"-1"

If key is given the new symbol will be prefixed with it.

Symbol.generate(:foo)  #=> :"foo-1"

TODO: Is the generated symbol format acceptable?

CREDIT: Trans


15
16
17
18
19
20
21
# File 'lib/core/facets/symbol/generate.rb', line 15

def self.generate(key=nil)
  key = key.to_sym if key
  @symbol_generate_counter ||= {}
  @symbol_generate_counter[key] ||= 0
  num = @symbol_generate_counter[key] += 1
  ("#{key}-%X" % num).to_sym
end

Instance Method Details

#/(path) ⇒ Object

Join with path as a file path.

  • path - The path component(s) to append. [#to_s]

Examples

(:merb / "string")   #=> "merb/string"
(:merb / :symbol)    #=> "merb/symbol"

Returns String of the receiver (as a path string), concatenated with path.


16
17
18
# File 'lib/core/facets/symbol/op_div.rb', line 16

def /(path)
  File.join(to_s, path.to_s)
end

#as_sObject

Convert symbol to string, apply string method and convert back to symbol via a fluent interface.

:HELLO.as_s.chomp('O')  #=> :HELL

12
13
14
15
16
# File 'lib/core/facets/symbol/as_s.rb', line 12

def as_s
  Functor.new do |op, *a|
    to_s.send(op, *a).to_sym
  end
end

#bang?Boolean

Symbol ends in `!`.

:a!.bang? #=> true
:a.bang?  #=> false

Returns:

  • (Boolean)

46
47
48
# File 'lib/core/facets/symbol/plain.rb', line 46

def bang?
  to_s[-1,1] == '!'
end

#call(*args, &block) ⇒ Object

Useful extension for &:symbol which makes it possible to pass arguments for method in block

['abc','','','def','ghi'].tap(&:delete.(''))#=> ['abc','def','ghi']


[1,2,3].map(&:to_s.(2))#=> ['1','10','11']


['abc','cdef','xy','z','wwww'].select(&:size.() == 4)#=> ['cdef', 'wwww']


['abc','aaA','AaA','z'].count(&:upcase.().succ == 'AAB')#=> 2


[%w{1 2 3 4 5},%w{6 7 8 9}].map(&:join.().length)#=> [5,4]

CREDIT: Ilya Vorontsov, Nobuyoshi Nakada


23
24
25
26
27
# File 'lib/core/facets/symbol/call.rb', line 23

def call(*args, &block)
  proc do |recv|
    recv.__send__(self, *args, &block)
  end
end

#chomp(seperator) ⇒ Object

Just like String#chomp.

:ab.chomp(:b)  #=> :a

CREDIT: Trans


9
10
11
# File 'lib/core/facets/symbol/chomp.rb', line 9

def chomp(seperator)
  to_s.chomp(seperator.to_s).to_sym
end

#clone?Boolean

Returns:

  • (Boolean)

67
# File 'lib/core/facets/object/dup.rb', line 67

def clone? ; false ; end

#dup!Object

Since Symbol is immutable it cannot be duplicated. For this reason #try_dup returns self.

:a.dup!  #=> :a

65
# File 'lib/core/facets/object/dup.rb', line 65

def dup!   ; self  ; end

#dup?Boolean

Returns:

  • (Boolean)

66
# File 'lib/core/facets/object/dup.rb', line 66

def dup?   ; false ; end

#lchomp(seperator) ⇒ Object

Just like String#lchomp.

:ab.lchomp(:a)  #=> :b

CREDIT: Trans


19
20
21
# File 'lib/core/facets/symbol/chomp.rb', line 19

def lchomp(seperator)
  to_s.reverse.chomp(seperator.to_s).reverse.to_sym
end

#not?Boolean

Does a symbol have a “not” sign?

"friend".to_sym.not?   #=> false
"~friend".to_sym.not?  #=> true

CREDIT: Trans

Returns:

  • (Boolean)

10
11
12
# File 'lib/core/facets/symbol/not.rb', line 10

def not?
  self.to_s.slice(0,1) == '~'
end

#plain?Boolean Also known as: reader?

Symbol does not end in `!`, `=`, or `?`.

:a.plain?   #=> true
:a?.plain?  #=> false
:a!.plain?  #=> false
:a=.plain?  #=> false

Returns:

  • (Boolean)

10
11
12
13
# File 'lib/core/facets/symbol/plain.rb', line 10

def plain?
  c = to_s[-1,1]
  !(c == '=' || c == '?' || c == '!')
end

#query?Boolean

Symbol ends in `?`.

:a?.query? #=> true
:a.query?  #=> false

Returns:

  • (Boolean)

37
38
39
# File 'lib/core/facets/symbol/plain.rb', line 37

def query?
  to_s[-1,1] == '?'
end

#setter?Boolean Also known as: writer?

Symbol ends in `=`.

:a=.setter? #=> true
:a.setter?  #=> false

Returns:

  • (Boolean)

24
25
26
# File 'lib/core/facets/symbol/plain.rb', line 24

def setter?
  to_s[-1,1] == '='
end

#succ(n = 1) ⇒ Object

Successor method for symobol. This simply converts the symbol to a string uses String#succ and then converts it back to a symbol.

:a.succ  #=> :b

TODO: Make this work more like a simple character dial?


13
14
15
16
17
18
19
# File 'lib/core/facets/symbol/succ.rb', line 13

def succ(n=1)
  s = self.to_s
  n.times do
    s = s.succ
  end
  s.to_sym
end

#thrown?true, false

Returns truew if the given block throws the symbol, otherwise false. Note that throw inside the block must be used in one-argument form.

Returns:

  • (true, false)

7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/core/facets/symbol/thrown.rb', line 7

def thrown?
  thrown = true
  catch(self) do
    begin
      yield
      thrown = false
    rescue ArgumentError => err     # 1.9 exception
      thrown = false if err.message.index('uncaught throw')
    rescue NameError => err         # 1.8 exception
      thrown = false if err.message.index('uncaught throw')
    end
  end
  thrown
end

#variablizeObject

Prepend an “@” to the beginning of a symbol to make a instance variable name. This also replaces non-valid characters with underscores.

:a.variablize  #=> :"@a"

8
9
10
11
# File 'lib/core/facets/symbol/variablize.rb', line 8

def variablize
  name = to_s.gsub(/\W/, '_')
  "@#{name}".to_sym
end

#[email protected]Object

Add a “not” sign to the front of a symbol.

(~:friend)  #=> :"~friend"

CREDIT: Trans


20
21
22
23
24
25
26
# File 'lib/core/facets/symbol/not.rb', line 20

def [email protected]
  if self.to_s.slice(0,1) == '~'
    "#{self.to_s[1..-1]}".to_sym
  else
    "~#{self}".to_sym
  end
end