Created a simple Class in ruby to help reading and writing property files
class JavaProps
attr :file, :properties
#Takes a file and loads the properties in that file
def initialize file
@file = file
@properties = {}
IO.foreach(file) do |line|
@properties[$1.strip] = $2 if line =~ /([^=]*)=(.*)\/\/(.*)/ || line =~ /([^=]*)=(.*)/
end
end
#Helpfull to string
def to_s
output = “File Name #{@file} \n”
@properties.each {|key,value| output += ” #{key}= #{value} \n” }
output
end
#Write a property
def write_property (key,value)
@properties[key] = value
end
#Save the properties back to file
def save
file = File.new(@file,”w+”)
@properties.each {|key,value| file.puts “#{key}=#{value}\n” }
end
end
6 responses so far ↓
Monkey Magic // October 30, 2008 at 4:09 pm |
What about escaping?
Tiago Romero Garcia // January 27, 2009 at 4:08 pm |
Hi! I am using your class in a project, and I would like to post it in my blog. I will put a link to here. Would you approve this? I hope to do so
Thanks!
devender // January 28, 2009 at 8:40 am |
Sure, I approve
Tiago Romero Garcia // January 27, 2009 at 4:09 pm |
By the way, my blog is: http://tiagoromero.wordpress.com/
If you agree, can you please post your response for me there?
Thanks again!
10 boas práticas em Ruby WATIR « Café com capuccino // February 10, 2009 at 8:09 am |
[...] utilizo para isto a seguinte classe leitora de arquivos properties de java, disponível neste link. Agradeço a Devender Gollapally por permitir a reprodução e utilização de seu código [...]
Flug USA // March 25, 2009 at 7:15 am |
Good work
As a java developper , I keep my eye on other languages that can play a role in java-based systems, among them ruby.That’s why I’m really interested in learning Ruby. I found your code really helpful .
I’m expecting more useful RUBY tips from you for java developpers