# Parsing key-value files

**URL:** <https://www.ruby-forum.com/t/parsing-key-value-files/56256>\
**Category:** Rails\
**Created:** [January 5, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/parsing-key-value-files/56256 "2006-01-05T19:57:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 5, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/parsing-key-value-files/56256/1 "2006-01-05T19:57:29Z")

</div>

Hi,

I am very very new to both Ruby and Ruby on Rails. I have been given a  
task to  
create a web application, which is able to edit individual entries in a  
key-value file. In other works, I would need to create an application  
which can  
parse a key-value file. Could someone guide me where I should start in a  
case  
like that? I was able to access and edit database table entries using  
Rails, but  
I don’t know how to parse these key-value files. Also, I was able to use  
Rails  
with a database because a tutorial showed me how to. I don’t know  
anything about  
Ruby. Will I have to write Ruby code in the controller of the  
application to  
parse a file? Is there an existing key-value file parsing method in  
Ruby? Google  
didn’t help much in answering these questions.

Thanks.

Vishal

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 1:37pm UTC](https://www.ruby-forum.com/t/parsing-key-value-files/56256/2 "2006-01-07T13:37:02Z")

</div>

On 1/5/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I don’t know anything about Ruby.

If you’re going to create a webapp, it seems like you’d want to know  
something about the language you’re using first. I’d suggest you read  
at least the first several sections of Programming Ruby, either the  
newer paper version or the older online version.

[http://www.rubycentral.com/book/](http://www.rubycentral.com/book/)

– James

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 1:37pm UTC](https://www.ruby-forum.com/t/parsing-key-value-files/56256/3 "2006-01-07T13:37:02Z")

</div>

Hi, Vishal,

for your problem YAML might be the right choice:  
[http://www.ruby-doc.org/core/classes/YAML.html](http://www.ruby-doc.org/core/classes/YAML.html)

> **[YAML.rb: YAML for Ruby](https://sourceforge.net/projects/yaml4r/)**
>
> Download YAML.rb: YAML for Ruby for free. YAML.rb is a YAML processor for the Ruby language, coded in pure Ruby. Its implementation is simple and small, designed to be used in place of PStore.

of course you could parse simple key-value-pairs with regular  
expressions…

Regards  
Jan

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 7, 2006, 1:37pm UTC](https://www.ruby-forum.com/t/parsing-key-value-files/56256/4 "2006-01-07T13:37:07Z")

</div>

If I were approaching this, I would probably avoid editing the  
key-value file directly, and instead storing the key/value pairs in an  
indexed database (if a flat-file was enough, then sqlite is probably  
sufficient). Then provide import/export/merge(if meaningful) support  
to/from the key/value file format. I don’t know what your keys and  
values look like, but if it’s CSV or XML then ruby includes enough  
library support to parse it out of the box. Yaml is pulled in by  
rails, and doing your own parsing with regular expressions is dead  
easy in ruby.

If it’s more complicated, then you might look into a recursive-descent  
parsing framework. I don’t have much experience doing this in ruby,  
but lemon and spirit are good choices in the c/c++ world and can do a  
lot of the heavy lifting for you on real parsing tasks, especially if  
performance matters.

Brian

On 1/5/06, Jan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Jan
> 
> > I don’t know how to parse these key-value files. Also, I was able to use Rails
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
The years ahead pick up their dark bags.  
They move closer. There’s a slight rise in the silence

## then nothing.

(If you’re receiving this in response to mail sent to  
removed\_email\_address@domain.invalid, don’t be concerned This is my new address,  
but mail will be forwarded here indefinitely)
