ruby - How to generate variable names -
i iterating through this_dir
,
this_dir = dir.new(".")
puts
ing each .rb file. folders (everything aren't .rb), open them, list contents, , set them variable. created array names
variable name from, , planned iterate through calling names_index
, adding 1
that.
names = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'n', 'm', 'o', 'p'] names_index = 0
unfortunately, closest thing know how array values puts
them, makes string.
this_dir.each |file| if file.include?("*.rb") puts file else .... end end
how turn array values variable names?
if want list of .rb
files in directory , subdirectories, use dir.glob
**
pattern recurse subdirectories, so:
dir.glob('./**/*.rb').each |file| puts file end
in general doesn't make sense dynamically create variable names purpose of storing many items. in such cases (when better solution dir.glob
above isn't available) should use array (or hash).
Comments
Post a Comment