Module: Kernel
- Defined in:
- lib/symbolic/plugins/var_name.rb,
lib/symbolic/extensions/kernel.rb
Overview
This is a simple module to give the name of the Ruby variable to the Symbolic::Variable
x = var #=> x = var :name => 'x')
It can also name multiple variables (but with no options then):
x, y = vars
This works with caller, and then need to be called directly
Instance Method Summary collapse
Instance Method Details
#_var ⇒ Object
8 |
# File 'lib/symbolic/plugins/var_name.rb', line 8 alias :_var :var |
#var(options = {}, &proc) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/symbolic/plugins/var_name.rb', line 9 def var(={}, &proc) unless .has_key? :name file, ln = caller[0].split(':') .merge!( :name => File.open(file) { |f| f.each_line.take(ln.to_i)[-1] }.match(/ \s*([[:word:]]+) \s*= \s*var/x ) { $1 }) end _var(, &proc) end |
#vars ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/symbolic/plugins/var_name.rb', line 27 def vars file, ln = caller[0].split(':') File.open(file) { |f| f.each_line.take(ln.to_i)[-1] }.match(/ ((?:\s*[[:word:]]+?,?)+) \s*= \s*vars/x ) { $1 }.scan(/([[:word:]]+)/).map { |capture| _var(:name => capture[0]) } end |