xref: /aosp_15_r20/external/libconfig/contrib/libconfig-ruby/README (revision 2e9d491483b805f09ea864149eadd5680efcc72a)
1*2e9d4914SAndroid Build Coastguard Worker# << Usage >>
2*2e9d4914SAndroid Build Coastguard Worker
3*2e9d4914SAndroid Build Coastguard Worker# you can feed this README to irb and see the result
4*2e9d4914SAndroid Build Coastguard Worker# $ irb README
5*2e9d4914SAndroid Build Coastguard Worker
6*2e9d4914SAndroid Build Coastguard Worker# IMPORTANT NOTICE:
7*2e9d4914SAndroid Build Coastguard Worker# be careful with big *fixnum* (plain int) values in configs
8*2e9d4914SAndroid Build Coastguard Worker# int is 32 bit, but ruby fixnum is only 31!
9*2e9d4914SAndroid Build Coastguard Worker# For example, 2100000000 will be read as -47483648.
10*2e9d4914SAndroid Build Coastguard Worker
11*2e9d4914SAndroid Build Coastguard Workerrequire 'rconfig'
12*2e9d4914SAndroid Build Coastguard Workerc = Config.new
13*2e9d4914SAndroid Build Coastguard Worker
14*2e9d4914SAndroid Build Coastguard Workerc.read!('test.cfg')
15*2e9d4914SAndroid Build Coastguard Worker# => IOError
16*2e9d4914SAndroid Build Coastguard Workerc.read('test.cfg')
17*2e9d4914SAndroid Build Coastguard Worker# => false
18*2e9d4914SAndroid Build Coastguard Worker
19*2e9d4914SAndroid Build Coastguard Workerp c['some_var']
20*2e9d4914SAndroid Build Coastguard Worker# => SettingNotFoundError
21*2e9d4914SAndroid Build Coastguard Worker# note: Config#lookup is alias for Config#[]
22*2e9d4914SAndroid Build Coastguard Worker
23*2e9d4914SAndroid Build Coastguard Workerc.append 'fixnum', Config::Fixnum.new(150)
24*2e9d4914SAndroid Build Coastguard Worker# #<Config::Fixnum...>
25*2e9d4914SAndroid Build Coastguard Worker
26*2e9d4914SAndroid Build Coastguard Workerf1 = Config::Fixnum.new(1)
27*2e9d4914SAndroid Build Coastguard Workerc.append 'another_fixnum', f1
28*2e9d4914SAndroid Build Coastguard Worker
29*2e9d4914SAndroid Build Coastguard Workerf2 = Config::Fixnum.new(256)
30*2e9d4914SAndroid Build Coastguard Workerc.append 'next_fixnum', f2
31*2e9d4914SAndroid Build Coastguard Worker
32*2e9d4914SAndroid Build Coastguard Workerp c.size
33*2e9d4914SAndroid Build Coastguard Worker# => 3
34*2e9d4914SAndroid Build Coastguard Worker
35*2e9d4914SAndroid Build Coastguard Workerc.delete(f1)            # by element
36*2e9d4914SAndroid Build Coastguard Workerc.delete(0)             # by index
37*2e9d4914SAndroid Build Coastguard Workerc.delete('next_fixnum') # by name
38*2e9d4914SAndroid Build Coastguard Worker# note: (at now) you cannot delete nested elements by Config#delete
39*2e9d4914SAndroid Build Coastguard Worker#       you can do c['nested.element'].parent.delete(c['nested.element'])
40*2e9d4914SAndroid Build Coastguard Worker
41*2e9d4914SAndroid Build Coastguard Workerp c.size
42*2e9d4914SAndroid Build Coastguard Worker# => 0
43*2e9d4914SAndroid Build Coastguard Worker
44*2e9d4914SAndroid Build Coastguard Workerl = Config::List.new
45*2e9d4914SAndroid Build Coastguard Workerc.append 'the_list', l
46*2e9d4914SAndroid Build Coastguard Worker
47*2e9d4914SAndroid Build Coastguard Workerl.append Config::String.new("abcdef")
48*2e9d4914SAndroid Build Coastguard Workerl << Config::Float.new(3.14)
49*2e9d4914SAndroid Build Coastguard Worker# note: Config::List#append and Config::Array#append both have
50*2e9d4914SAndroid Build Coastguard Worker#       aliases Config::[Aggregate]#<<
51*2e9d4914SAndroid Build Coastguard Worker
52*2e9d4914SAndroid Build Coastguard Workerp l.name
53*2e9d4914SAndroid Build Coastguard Worker# => "the_list"
54*2e9d4914SAndroid Build Coastguard Worker
55*2e9d4914SAndroid Build Coastguard Workerp l.index
56*2e9d4914SAndroid Build Coastguard Worker# => 0
57*2e9d4914SAndroid Build Coastguard Worker
58*2e9d4914SAndroid Build Coastguard Workerp l.root?
59*2e9d4914SAndroid Build Coastguard Worker# => false
60*2e9d4914SAndroid Build Coastguard Worker
61*2e9d4914SAndroid Build Coastguard Workerp l.size
62*2e9d4914SAndroid Build Coastguard Worker# => 3
63*2e9d4914SAndroid Build Coastguard Worker
64*2e9d4914SAndroid Build Coastguard Workerl[0].format = Config::FORMAT_HEX
65*2e9d4914SAndroid Build Coastguard Worker
66*2e9d4914SAndroid Build Coastguard Workerp l[1].value
67*2e9d4914SAndroid Build Coastguard Worker# => 3.14
68*2e9d4914SAndroid Build Coastguard Worker
69*2e9d4914SAndroid Build Coastguard Workerl[1].value = 2.71828
70*2e9d4914SAndroid Build Coastguard Worker
71*2e9d4914SAndroid Build Coastguard Workerc.write 'test.cfg'
72*2e9d4914SAndroid Build Coastguard Worker
73*2e9d4914SAndroid Build Coastguard Worker# you will get test.cfg with following contents:
74*2e9d4914SAndroid Build Coastguard Worker#
75*2e9d4914SAndroid Build Coastguard Worker# the_list = ( "abcdef", 2.71828, 0x2A );
76*2e9d4914SAndroid Build Coastguard Worker#
77