Read symbols from external file

=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

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 can simply eval the
string:

str = “:symbol_1”
str = eval str
p str

–output:–
:symbol_1

Otherwise, you need to remove the leading colon and then you can 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 then 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”

On Fri, May 27, 2011 at 7:24 AM, Agent M. [email protected]
wrote:

declared right on top of the little program below.
:symbol_1,:Tiger,
when:symbol_1 then puts <<_
_
end
end


Posted via http://www.ruby-forum.com/.

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

=begin
Thank you so much! File “zoo.txt” contains the following:

symbol_3 Black_Widow
symbol_1 Zebra
symbol_2 Pinquin
symbol_2 Flamingo
symbol_0 Steenbok
symbol_1 Tiger

The output of the little program below is:

 Symbol symbol_0: Feed the Steenbok
 Symbol symbol_1: Greet the Tiger
 Symbol symbol_1: Greet the Zebra
 Symbol symbol_2: Meet the Flamingo
 Symbol symbol_2: Meet the Pinquin
 Symbol symbol_3: Smile politely to the Black_Widow

=end

IO.read(“zoo.txt”).split.each_slice(2).sort.each do|a|
case a[0].to_sym
when:symbol_0 then puts <<_
Symbol #{a[0]}: Feed the #{a[1]}
_
when:symbol_1 then puts <<_
Symbol #{a[0]}: Greet the #{a[1]}
_
when:symbol_2 then puts <<_
Symbol #{a[0]}: Meet the #{a[1]}
_
when:symbol_3 then puts <<_
Symbol #{a[0]}: Smile politely to the #{a[1]}
_
end
end

=begin
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
“animals.txt”,
but I don’t know how. Nor do I know how to write animals.txt.

Can you help me with this?
=end

=begin
Thank you for your answers! I tried but have not yet fixed it. There
is a file “zoo.txt” now with this content:

:symbol_1 :Zebra
:symbol_1 :Tiger
:symbol_2 :Pinquin
:symbol_2 :Flamingo
:symbol_3 :Black_Widow

I replaced the array literal on top of the program with two lines
where I read the file and convert the strings to symbols. But running
the program doesn’t give any results…
=end

f=File.read(“zoo.txt”).split
f.each{|a|a.to_sym}.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