ruby - How to generate variable names -


i iterating through this_dir,

this_dir = dir.new(".") 

putsing 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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

how to prompt save As Box in Excel Interlop c# MVC 4 -

xslt 1.0 - How to access or retrieve mets content of an item from another item? -