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
# File lib/core/facets/symbol/generate.rb, line 15
15: def self.generate(key=nil)
16: key = key.to_sym if key
17: @symbol_generate_counter ||= {}
18: @symbol_generate_counter[key] ||= 0
19: num = @symbol_generate_counter[key] += 1
20: ("#{key}-%X" % num).to_sym
21: end
Join with path as a file path.
:merb / "core_ext" #=> "merb/core_ext" :merb / :core_ext / :string #=> "merb/core_ext/string"
@param [#] path The path component(s) to append.
@return [String] The receiver (as path string), concatenated with path.
@api public
# File lib/core/facets/symbol/op_div.rb, line 14
14: def /(path)
15: File.join(to_s, path.to_s)
16: end
Just like String#chomp.
:ab.chomp(:b) #=> :a
CREDIT: Trans
# File lib/core/facets/symbol/chomp.rb, line 9
9: def chomp(seperator)
10: to_s.chomp(seperator.to_s).to_sym
11: end
# File lib/core/facets/duplicable.rb, line 25
25: def clone? ; false ; end
# File lib/core/facets/duplicable.rb, line 24
24: def dup? ; false ; end
Just like String#lchomp.
:ab.lchomp(:a) #=> :b
CREDIT: Trans
# File lib/core/facets/symbol/chomp.rb, line 19
19: def lchomp(seperator)
20: to_s.reverse.chomp(seperator.to_s).reverse.to_sym
21: end
Does a symbol have a “not” sign?
"friend".to_sym.not? #=> false "~friend".to_sym.not? #=> true
CREDIT: Trans
# File lib/core/facets/symbol/not.rb, line 10
10: def not?
11: self.to_s.slice(0,1) == '~'
12: end
# File lib/core/facets/symbol/plain.rb, line 3 3: def plain? 4: c = to_s[1,1] 5: !(c == '=' || c == '?') 6: end
# File lib/core/facets/symbol/plain.rb, line 12
12: def query?
13: to_s[1,1] == '?'
14: end
Convert symbol to string, apply string method and convert back to symbol via a fluent interface.
:HELLO.re_s.downcase
# File lib/core/facets/symbol/re_s.rb, line 11
11: def re_s
12: @re_s ||= Functor.new do |op, *a|
13: to_s.send(op, *a).to_sym
14: end
15: end
# File lib/core/facets/symbol/plain.rb, line 8
8: def setter?
9: to_s[1,1] == '='
10: end
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?
# File lib/core/facets/symbol/succ.rb, line 13
13: def succ
14: self.to_s.succ.intern
15: end
Does the block throw the symbol?
# File lib/core/facets/symbol/thrown.rb, line 5
5: def thrown?
6: catch(self) do
7: begin
8: yield
9: true
10: rescue ArgumentError => err # 1.9 exception
11: #msg += ", not #{err.message.split(/ /).last}"
12: false
13: rescue NameError => err # 1.8 exception
14: #msg += ", not #{err.name.inspect}"
15: false
16: end
17: end
18: end
Turn a symbol into a proc calling the method to which it refers.
up = :upcase.to_proc
up.call("hello") #=> HELLO
More useful is the fact that this allows & to be used to coerce Symbol into Proc.
%w{foo bar qux}.map(&:upcase) #=> ["FOO","BAR","QUX"]
[1, 2, 3].inject(&:+) #=> 6
And other conveniences such as:
%{john terry fiona}.map(&:capitalize) # -> %{John Terry Fiona}
sum = numbers.inject(&:+)
TODO: This will be deprecated as of Ruby 1.9, since it will become standard Ruby.
CREDIT: Florian Gross (orignal), Nobuhiro Imai (current)
# File lib/core/facets/symbol/to_proc.rb, line 26
26: def to_proc
27: Proc.new{|*args| args.shift.__send__(self, *args)}
28: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.