Im trying to set the IRB prompt mode via IRB.conf[:PROMPT_MODE] = mode
where mode is a variable pulled in as a string from a KEY=VALUE config file. I need to convert the contents of the string to a symbol named the value of the string. Is this possible? My source (except the config file.) is located at Github. The config file looks like this:
HistoryCount=1000
HistoryFile=~/.rbsh/history
Prompt=SIMPLE
Of course yes:
To convert a String object to a Symbol object:
value = 'Hello world, this is a string'
p value.intern # => :"Hello world, this is a string"
p value.intern.class # => Symbol
value = 'Hello world, this is a string'
p value.to_sym # => :"Hello world, this is a string"
p value.to_sym.class # => Symbol
In your case, you read the config file, process the file, and assign it to a variable (I am using puts here):
puts IO.readlines(File.join(ENV['HOME'], %w(.rbsh history))).map { |x| x.strip.split(/\s*=\s*/) }.to_h { |x, y| [x, y.intern] }
Output on STDOUT
{"HistoryCount"=>:"1000", "HistoryFile"=>:"~/.rbsh/history", "Prompt"=>:SIMPLE}
Donβt forget to use exception handling or file checking before you work with the file
Does this help? πβ
Thanks! This helped a lot! Exception handling is on a todo list but it definately will include that before I add it to the gem repo.
1 Like