Python has a vars () method that returns a dictionary of local variable names in the form of keys and their values, so you can do something like this:
a = 1 b = 2 "%(a)s %(b)s" % vars()
Is there an analogue of vars () in Ruby? The closest design I can think of is
local_variables.inject({}) {|res,var| res[var] = eval(var.to_s); res}
- , . , , . , , - , - , . ( , vars ) .
#!/usr/bin/env ruby module Kernel def vars(b) Hash[ (b.eval('local_variables')-[:_]). map{|v| [v,b.eval(v.to_s)]} ] end end a = 10 puts vars(binding)
, ,
"#{a} #{b}"
, vars()?
vars()
Source: https://habr.com/ru/post/1761137/More articles:sed or awk to print lines between words - sedConvert text field text to integer - c #Why are my local dlls permanently loaded after installing _NT_SYMBOL_PATH? - c ++strange behavior with the sentence "IN" works - stringRead database.yml from inside ActiveRecord migration - ruby | fooobar.comCreate a function to scroll through method properties in C #? - functionОпределение проблем, связанных с эндией - c++Encrypt a string in Java so that C can decrypt it without any additions to the decrypted text? - javaNeed help with perl peak detection - perl@Override for interface methods causes JSP compilation to fail - javaAll Articles