Interact with a symbol via a variable

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:

:clock1:

value = 'Hello world, this is a string'
p value.intern          # => :"Hello world, this is a string"
p value.intern.class    # => Symbol

:clock2:

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 :outbox_tray:

{"HistoryCount"=>:"1000", "HistoryFile"=>:"~/.rbsh/history", "Prompt"=>:SIMPLE}

Donโ€™t forget to use exception handling or file checking before you work with the file :man_factory_worker::woman_factory_worker:


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