Class: Symbol
Overview
Author: Stefano Harding <[email protected]> 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.
Class Method Summary collapse
-
.generate(key = nil) ⇒ Object
Generate a unique symbol.
Instance Method Summary collapse
-
#/(other) ⇒ String
Join with o as a file path.
-
#bang? ⇒ Boolean
Symbol ends in ‘!`.
-
#call(*args, &block) ⇒ Object
Useful extension for &:symbol which makes it possible to pass arguments for method in block.
- #clone? ⇒ Boolean
-
#dup! ⇒ Object
Since Symbol is immutable it cannot be duplicated.
- #dup? ⇒ Boolean
-
#not? ⇒ Boolean
Does a symbol have a “not” sign?.
-
#plain? ⇒ Boolean
(also: #reader?)
Symbol does not end in ‘!`, `=`, or `?`.
-
#query? ⇒ Boolean
Symbol ends in ‘?`.
-
#setter? ⇒ Boolean
(also: #writer?)
Symbol ends in ‘=`.
- #to_proc ⇒ Object
-
#try_dup ⇒ Object
Override this in a child if it cannot be dup’ed.
-
#~@ ⇒ Object
Add a “not” sign to the front of a symbol.
Class Method Details
.generate(key = nil) ⇒ Object
130 131 132 133 134 135 136 |
# 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 |
Instance Method Details
#/(other) ⇒ String
Join with o as a file path.
50 51 52 |
# File 'lib/garcon/core_ext/symbol.rb', line 50 def /(other) File.join(self.to_s, other.to_s) end |
#bang? ⇒ Boolean
Symbol ends in ‘!`.
:a!.bang? #=> true
:a.bang? #=> false
97 98 99 |
# File 'lib/garcon/core_ext/symbol.rb', line 97 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]
164 165 166 167 168 |
# File 'lib/garcon/core_ext/symbol.rb', line 164 def call(*args, &block) proc do |recv| recv.__send__(self, *args, &block) end end |
#dup! ⇒ Object
Since Symbol is immutable it cannot be duplicated. For this reason #try_dup returns self.
:a.dup! #=> :a
33 |
# File 'lib/garcon/core_ext/symbol.rb', line 33 def dup! ; self ; end |
#not? ⇒ Boolean
Does a symbol have a “not” sign?
"friend".to_sym.not? #=> false
"~friend".to_sym.not? #=> true
106 107 108 |
# File 'lib/garcon/core_ext/symbol.rb', line 106 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
61 62 63 64 |
# File 'lib/garcon/core_ext/symbol.rb', line 61 def plain? c = to_s[-1,1] !(c == '=' || c == '?' || c == '!') end |
#query? ⇒ Boolean
Symbol ends in ‘?`.
:a?.query? #=> true
:a.query? #=> false
88 89 90 |
# File 'lib/garcon/core_ext/symbol.rb', line 88 def query? to_s[-1,1] == '?' end |
#setter? ⇒ Boolean Also known as: writer?
Symbol ends in ‘=`.
:a=.setter? #=> true
:a.setter? #=> false
75 76 77 |
# File 'lib/garcon/core_ext/symbol.rb', line 75 def setter? to_s[-1,1] == '=' end |
#to_proc ⇒ Object
140 141 142 143 |
# 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 ⇒ Object
Override this in a child if it cannot be dup’ed
24 25 26 |
# File 'lib/garcon/core_ext/symbol.rb', line 24 def try_dup self end |
#~@ ⇒ Object
Add a “not” sign to the front of a symbol.
(~:friend) #=> :"~friend"
114 115 116 117 118 119 120 |
# File 'lib/garcon/core_ext/symbol.rb', line 114 def ~@ if self.to_s.slice(0,1) == '~' "#{self.to_s[1..-1]}".to_sym else "~#{self}".to_sym end end |