Skip to content
DuelitDev edited this page Feb 22, 2022 · 1 revision

Install

gem install XProperties

Documentation

Properties

load

Loads a property list(key and element pairs) from the .properties file. The file is assumed to use the ISO-8859-1(Latin1) character encoding.

param file_name - .properties file name.
exception IOError - When an error occurs while reading the file.
exception ArgumentError - When the file contains a malformed Unicode escape sequence.

Examples

# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"

Defined: 1.0.0


save

Saves a property list(key and element pairs) from the .properties file. The file will be written in ISO-8859-1(Latin1) character encoding.

param file_name - .properties file name.
exception IOError - When an error occurs while writing the file.

Examples

# example.rb
require 'XProperties'


prop = Properties.new
prop["message"] = "Hello, World!"
prop.save "example.properties"

Defined: 1.0.0


load_from_xml

Loads a property list(key and element pairs) from the .xml file.

The XML document must have the following DOCTYPE declaration:
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

param file_name - .xml file name.
exception IOError - When an error occurs while reading the file.
exception ParseException - When the XML file is malformed.

Examples

# example.rb
require 'XProperties'


prop = Properties.new
prop.load_from_xml "example.properties.xml"

Defined: 1.0.0


save_to_xml

Saves a property list(key and element pairs) to the .xml file.

The XML document must have the following DOCTYPE declaration:
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

param file_name - .xml file name.
exception IOError - When an error occurs while writing the file.

Examples

# example.rb
require 'XProperties'


prop = Properties.new
prop["message"] = "Hello, World!"
prop.save_to_xml "example.properties.xml"

Defined: 1.0.0


set_property

Setting the value of a property.

param key - Property name.
param value - Value to set for the property.

Examples

# example.rb
require 'XProperties'


prop = Properties.new
prop.set_property "message1", "Hello, World!"
prop["message2"] = "Hello, World?"  # Recommended
prop.save "example.properties"
# example.properties
message1=Hello, World!
message2=Hello, World?

Alias: []=(key, value)
Defined: 1.0.0


get_property

Getting the value of a property.

param key - Property name.
param default - Default value if property does not exist.
exception KeyError - When property does not exist.
return - Property Value.

Examples

# example.properties
message=Hello, World!
# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"
puts prop.get_property("message")  # Hello, World!
puts prop.get_property("default", "Does not exist.")  # Does not exist.
puts prop["message"]  # Recommended

Alias: [](key)
Defined: 1.0.0


delete_property

Deleting the value of a property.

param key - Property name.
exception KeyError - When property does not exist.

Examples

# example.properties
message=Hello, World!
notuse=abcdefghijklm
# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"
prop.delete_property "notuse"
prop.save "example.properties"
# example.properties
message=Hello, World!

Defined: 1.0.0


clear

Remove all properties.

Examples

# example.properties
message=Hello, World!
# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"
prop.clear
prop.save "example.properties"
# example.properties

Defined: 1.1.0


keys

Getting the list of properties name.

return - List of properties name.

Examples

# example.properties
message=Hello, World!
test=It is a test.
# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"
puts prop.keys  # ["message", "test"]

Defined: 1.0.0


values

Getting the list of properties value.

return - List of properties value.

Examples

# example.properties
message=Hello, World!
test=It is a test.
# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"
puts prop.values  # ["Hello, World!", "It is a test."]

Defined: 1.0.0


items

Getting the list of properties key-value pair.

return - List of properties key-value pair.

Examples

# example.properties
message=Hello, World!
# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"
puts prop.items  # [["message", "Hello, World!"]]
# or you can use iterator.
prop.each do |i|
  puts i
end

Defined: 1.0.0


count

Get the number of properties.

return - The number of properties.

Examples

# example.properties
index0=Hello, World!
index1=Hello, World!
index2=Hello, World!
index3=Hello, World!
index4=Hello, World!
# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"
puts prop.count  # 5

Defined: 1.0.0


contains?

Returns a value whether the key exists.

return - Boolean value of key existence.

Examples

# example.properties
message=Hello, World!
# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"
puts prop.contains?("message")  # True
puts prop.contains?("username")  # False

Defined: 1.0.0


equals?

Returns a value whether two object instances are equal.

param other - Other object to compare.
return - Boolean value of whether two object instances are equal.

Examples

# example.properties
message=Hello, World!
# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"
puts prop.equals?(prop)  # True
puts prop.equals?("not prop")  # False
puts prop == prop  # Recommended

Alias: eql?(other)
Defined: 1.0.0


to_string

Convert instance to string.

return - Converted string.

Examples

# example.properties
message=Hello, World!
# example.rb
require 'XProperties'


prop = Properties.new
prop.load "example.properties"
puts prop.to_string  # {"message": "Hello, World!"}
puts prop.to_s  # Recommended

Alias: to_s
Defined: 1.0.0