Skip to content

Instantly share code, notes, and snippets.

@dmichael
Created August 14, 2009 13:56
Show Gist options
  • Select an option

  • Save dmichael/167849 to your computer and use it in GitHub Desktop.

Select an option

Save dmichael/167849 to your computer and use it in GitHub Desktop.
# There really is no need for this class. One could use java.util.Properties in JRuby,
# but figuring out how to use it from JRuby was more cumbersome than just writing this class.
# Parser stolen from
# http://devender.wordpress.com/2006/05/01/reading-and-writing-java-property-files-with-ruby/
class JavaProperties < Hash
attr_accessor :file
# Takes a file and loads the properties in that file
def initialize(file)
@file = file
IO.foreach(file) do |line|
self[$1.strip] = $2.strip if line =~ /([^=]*)=(.*)\/\/(.*)/ || line =~ /([^=]*)=(.*)/
end
end
# A getter method for kicks
def get(key)
self[key]
end
# A setter method for kicks
def set(key, value)
self[key] = value
end
#Save the properties back to file
def save
file = File.new(@file,"w+")
self.each {|key,value| file.puts "#{key}=#{value}\n" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment