Read symbols from external file

A

Agent Mulder

=begin
Hello,

I parse an array of symbols to output some program code. In reality I
map ALT, CTRL and SHIFT declarations to VK_KEYS in Java. But the
principle is the same. It is not a hash, a :symbol_* can appear more
than once.

The list of symbols and what it applies to (ie SHIFT + VK_HOME) is a
straight array that I slice() in pairs just before I use it. It is
declared right on top of the little program below.

I want to read in these ruby symbols from an external file
"zoo.txt",but I don't know how. Nor do I know how to write
file zoo.txt.

Can you help me with this?
=end

[
:symbol_1,:Zebra,
:symbol_1,:Tiger,
:symbol_2,:pinquin,
:symbol_2,:Flamingo,
:symbol_3,:Black_Widow,
].
each_slice(2).each do|b|
case b[0]
when:symbol_0 then puts <<_

Symbol Zero: #{b[0]}-> #{b[1]}
_
when:symbol_1 then puts <<_

Symbol One: #{b[0]}-> #{b[1]}
_
when:symbol_2 then puts <<_

Symbol Two: #{b[0]}-> #{b[1]}
_
when:symbol_3 then puts <<_

Symbol Three: #{b[0]}-> #{b[1]}
_
end
end
 
7

7stud --

You can only read in strings from a file, and you can only write out
strings to a file.

If you read in the string ":symbol_1", you have to convert that to a
symbol. If the file is from a trusted source, you could simply eval the
string:

str = ":symbol_1"
str = eval str
p str

--output:--
:symbol_1

Otherwise, you can can remove the leading colon and then convert the
string to a symbol with to_sym():

str = ":symbol_1"
sym = str[1..-1].to_sym
p sym

--output:--
:symbol_1

To convert a symbol to a string and add a leading colon, so that you can
write the string to a file, you can do this:

sym = :symbol_1
p ":#{sym.to_s}"

--output:--
":symbol_1"
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

=begin
Hello,

I parse an array of symbols to output some program code. In reality I
map ALT, CTRL and SHIFT declarations to VK_KEYS in Java. But the
principle is the same. It is not a hash, a :symbol_* can appear more
than once.

The list of symbols and what it applies to (ie SHIFT + VK_HOME) is a
straight array that I slice() in pairs just before I use it. It is
declared right on top of the little program below.

I want to read in these ruby symbols from an external file
"zoo.txt",but I don't know how. Nor do I know how to write
file zoo.txt.

Can you help me with this?
=end

[
:symbol_1,:Zebra,
:symbol_1,:Tiger,
:symbol_2,:pinquin,
:symbol_2,:Flamingo,
:symbol_3,:Black_Widow,
].
each_slice(2).each do|b|
case b[0]
when:symbol_0 then puts <<_

Symbol Zero: #{b[0]}-> #{b[1]}
_
when:symbol_1 then puts <<_

Symbol One: #{b[0]}-> #{b[1]}
_
when:symbol_2 then puts <<_

Symbol Two: #{b[0]}-> #{b[1]}
_
when:symbol_3 then puts <<_

Symbol Three: #{b[0]}-> #{b[1]}
_
end
end

# I think parsing the file is the wrong approach.
# I'd just use a real data format like YAML.
# (I'd probably also not store these as arrays,
# they look like good candidates for hashes or
# structs, because remembering what position 0 is,
# and what position 1 is will become a headache)

require 'yaml'
file = DATA
symbols = YAML.load file

symbols.each do |b|
case b[0]
when :symbol_0 then puts "Symbol Zero: #{b[0]} -> #{b[1]}"
when :symbol_1 then puts "Symbol One: #{b[0]} -> #{b[1]}"
when :symbol_2 then puts "Symbol Two: #{b[0]} -> #{b[1]}"
when :symbol_3 then puts "Symbol Three: #{b[0]} -> #{b[1]}"
end
end


__END__
---
- - :symbol_1
- :Zebra
- - :symbol_1
- :Tiger
- - :symbol_2
- :pinquin
- - :symbol_2
- :Flamingo
- - :symbol_3
- :Black_Widow
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,320
Messages
2,571,663
Members
48,471
Latest member
Jlspropest

Latest Threads

Top