Saturday 15 January 2011

ruby - Using require to use file with hash -



ruby - Using require to use file with hash -

i trying refer hash written in file, , tried:

require './filewithhash' #this file has hash puts name_hash['somename'] #just trying access index

and got undefined local variable or method error.

the problem when requiring file in ruby, local variables out of scope. hence any local variables defined in required file not available later on.

solutions:

make programme object oriented , add together hash field in class make variable constant (like this: name_hash) make variable global (like this: $name_hash) make variable instance variable (like this: @name_hash)

if programme simple, take alternative 3. if it's suppose grow, 1 best choice.

ruby

No comments:

Post a Comment