class Symbol

Author: Stefano Harding <riddopic@gmail.com> License: Apache License, Version 2.0 Copyright: © 2014-2015 Stefano Harding

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Public Class Methods

generate(key = nil) click to toggle source

Generate a unique symbol.

Symbol.generate  #=> :"-1"

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

Symbol.generate(:foo)  #=> :"foo-1"
# File lib/garcon/core_ext/symbol.rb, line 130
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

Public Instance Methods

/(other) click to toggle source

Join with o as a file path.

@example

:chef/'provider' # => 'chef/provider'
:chef/ :provider # => 'chef/provider'

@param [#to_s] other

Path component(s) to join with receiver.

@return [String]

Receiver joined with other as a file path.

@api public

# File lib/garcon/core_ext/symbol.rb, line 50
def /(other)
  File.join(self.to_s, other.to_s)
end
bang?() click to toggle source

Symbol ends in ‘!`.

:a!.bang? #=> true
:a.bang?  #=> false
# File lib/garcon/core_ext/symbol.rb, line 97
def bang?
  to_s[-1,1] == '!'
end
call(*args, &block) click to toggle source

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]
# File lib/garcon/core_ext/symbol.rb, line 164
def call(*args, &block)
  proc do |recv|
    recv.__send__(self, *args, &block)
  end
end
clone?() click to toggle source
# File lib/garcon/core_ext/symbol.rb, line 35
def clone? ; false ; end
dup!() click to toggle source

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

:a.dup!  #=> :a
# File lib/garcon/core_ext/symbol.rb, line 33
def dup!   ; self  ; end
dup?() click to toggle source
# File lib/garcon/core_ext/symbol.rb, line 34
def dup?   ; false ; end
not?() click to toggle source

Does a symbol have a “not” sign?

"friend".to_sym.not?   #=> false
"~friend".to_sym.not?  #=> true
# File lib/garcon/core_ext/symbol.rb, line 106
def not?
  self.to_s.slice(0,1) == '~'
end
plain?() click to toggle source

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

:a.plain?   #=> true
:a?.plain?  #=> false
:a!.plain?  #=> false
:a=.plain?  #=> false
# File lib/garcon/core_ext/symbol.rb, line 61
def plain?
  c = to_s[-1,1]
  !(c == '=' || c == '?' || c == '!')
end
Also aliased as: reader?
query?() click to toggle source

Symbol ends in ‘?`.

:a?.query? #=> true
:a.query?  #=> false
# File lib/garcon/core_ext/symbol.rb, line 88
def query?
  to_s[-1,1] == '?'
end
reader?()

Alias for ‘#plain?` method. Likely this should have been the original and only name, but such is life.

Alias for: plain?
setter?() click to toggle source

Symbol ends in ‘=`.

:a=.setter? #=> true
:a.setter?  #=> false
# File lib/garcon/core_ext/symbol.rb, line 75
def setter?
  to_s[-1,1] == '='
end
Also aliased as: writer?
to_proc() click to toggle source
# File lib/garcon/core_ext/symbol.rb, line 140
def to_proc
  proc { |obj, args| obj.send(self, *args) }
  # lambda { |obj, args=nil| obj.send(self, *args) }
end
try_dup() click to toggle source

Override this in a child if it cannot be dup’ed

@return [Object]

# File lib/garcon/core_ext/symbol.rb, line 24
def try_dup
  self
end
writer?()

Alias for ‘#setter?` method. Likely this should have been the original and only name, but such is life.

Alias for: setter?