Understanding YAML and this practice in general

Discussion in 'Ruby' started by Fily Salas, Mar 15, 2011.

  1. Fily Salas

    Fily Salas Guest

    Hi,

    I=E2=80=99m currently reading the book called =E2=80=9CLearn to Program=E2=
    =80=9D and I=E2=80=99m excited
    to see a language like Ruby with a clear syntax and probably easier to
    understand even for a newbie like me who have very little programming
    experience, in fact I already go stocked I would like someone to help me
    understand this.

    In chapter 11 (Reading and Writing, Saving and Loading,=E2=80=A6) the aut=
    hor
    demonstrates how you can save the output of your programs and I started
    wondering when and why you would need to save the output of a program?

    I know its probably too early to ask this kind of questions but I really
    started wondering since he started talking about YAML which apparently
    can be used in multiple languages and I'm assuming this is an important
    part of programming?

    Can someone be so kind and explain me when and why you would need YAML?

    Is this a common practice when writing a program where you dont have
    access to a database and which requires little data management?

    This is some code sample, this actually creates a file called
    ListerQuote.txt and saves the output string.
    Code:
    filename  =3D 'ListerQuote.txt'
    test_string =3D 'I promise that Iend valid
    swear absolutely that ' + 'I will never mention gazpacho soup again.'
    
    File.open filename, 'w' do |f|
    f.write test_string
    end
    read_string =3D File.read filename
    puts(read_string =3D=3D test_string)
    Sorry if my question doesn't make too much sense but I'm coming from a
    web development world with only some knowledge in Actionscript 3.0,
    Javascript, CSS and HTML.

    Thanks a lot!

    -- =

    Posted via http://www.ruby-forum.com/.=
    Fily Salas, Mar 15, 2011
    #1
    1. Advertising

  2. Fily Salas

    7stud -- Guest

    Fily Salas wrote in post #987440:
    > I started
    > wondering when and why you would need to save the output of a program?
    >
    > Sorry if my question doesn't make too much sense but I'm coming from a
    > web development world with only some knowledge in Actionscript 3.0,
    > Javascript, CSS and HTML.


    Have you ever heard of a logfile? The idea is that something monitors a
    program you are running and logs information to a file about various
    things that occur while the program is running. You can then open the
    file and read the file to see what happened while your programming was
    running. With a unix program like tail, you can even open the file and
    watch the changes to the file in real time.

    Do you know what a cookie is? It is a file on a user's computer that
    contains a short bit of information. Sometimes a javascript program
    writes output in the form of a cookie.

    Suppose you run a business, and at the end of every day your employee
    enters information about every transaction in a file. The information
    contains the customer's name and the dollar amount purchased--one line
    in the file for every transaction. You ask your ruby programmer to
    write a program that reads all the transaction files and print out the
    total amount each customer spent that month. The program must read each
    transaction and record all the transactions for each customer, i.e. one
    customer could have 100 separate transactions spread through out the
    files. The ruby programmer could output the totals to another file,
    arrange them alphabetically by customer name, and then send the file to
    you my email.

    Video games store totals and high scores to files. TV's store favorite
    channels and programs to be recorded in files. It would be easier to
    try and count the programs that don't store output to files...in fact I
    can't think of a single one.

    > I know its probably too early to ask this kind of questions but I really
    > started wondering since he started talking about YAML which apparently
    > can be used in multiple languages and I'm assuming this is an important
    > part of programming?
    >
    > Can someone be so kind and explain me when and why you would need YAML?
    >


    At the heart of your question is the question of data persistence--in
    other words how can you make data persist from one program run to the
    other. Anyone can make a string persist by writing a string to a file
    and reading it back later, but what about arrays and even
    objects(=things that store both data and functions/methods. How can you
    store those in a file and then read them back later? YAML and the many
    other programs that "serialize" data allow you to easily store things
    like arrays, hashes/dictionaries, and objects to a file and then
    reconstitute them the next time you run a program. YAML's claim to fame
    is that the file it creates is human readable. In the beginning, data
    serializers stored what looked like random characters in a file. They
    were actually complex codes, that a computer could decipher.

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Mar 15, 2011
    #2
    1. Advertising

  3. Fily Salas

    Fily Salas Guest

    Thank you for your reply! I now have a better idea as to what YAML does
    and its big plus, it basically makes the store objects human readable,
    even though I still don't know have a clear picture as to where or how
    this will be used later, but I guess the answer will come later when I
    gain more experience.

    Again I don't have too much experience with high level languages other
    than Actionscript 3.0 and JavaScript.

    Exited learning this new world!

    Thanks a lot!

    --
    Posted via http://www.ruby-forum.com/.
    Fily Salas, Mar 15, 2011
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    On Tue, Mar 15, 2011 at 11:48 AM, Fily Salas <> wrote:

    > Thank you for your reply! I now have a better idea as to what YAML does
    > and its big plus, it basically makes the store objects human readable,
    > even though I still don't know have a clear picture as to where or how
    > this will be used later, but I guess the answer will come later when I
    > gain more experience.
    >
    > Again I don't have too much experience with high level languages other
    > than Actionscript 3.0 and JavaScript.
    >
    >

    Hi Fily,
    it is often very useful to save the state of your program (or just a
    piece of it to the hard disk). You usually do this for the following
    reasons:
    - you need to restore the state of the program after you have switched it
    off and restarted it
    - you need to save user operations and work in a manner that you can easily
    load

    Using traditional means (databases, export formats etc.) this process is
    actually technically challenging to do. It usually involves writing a lot of
    code yourself to make the transformations, and dealing with any corruption.

    YAML on the other hand, saves your Ruby objects themselves. Its
    representation is standardised, so saving the state of your Ruby objects is
    simply a manner of passing the object to the appropriate function.
    Restoring is the reverse.

    It is an extremely simple procedure that is useful in a lot of contexts (it
    is not suitable for large sets of data, or heavy use).

    For instance, the YAML file can be moved between machines and used to
    replicate the data on another instance of the running program, either by
    manually transferring the file, or sending it over the network between the
    computers themselves.

    Also YAML files are human readable and human editable, which is also an
    advantage - especially if you are using them for compiling test data, or
    exporting the state of a running program (could be handy for debugging
    crashes).

    YAML is not the only technique for dumping the state of a program in Ruby
    (and these techniques are not Ruby specific either). In the JavaScript
    world, JSON fulfills almost exactly the same purpose. I believe that
    measures have been taken to make JSON and YAML grammatically compatible.

    regards,
    Richard



    --
    http://richardconroy.blogspot.com | http://twitter.com/RichardConroy
    Richard Conroy, Mar 15, 2011
    #4
  5. Fily Salas

    Fily Salas Guest

    Fily Salas, Mar 15, 2011
    #5
  6. Fily Salas:

    > I now have a better idea as to what YAML does and its big
    > plus, it basically makes the store objects human readable,
    > even though I still don't know have a clear picture as
    > to where or how this will be used later, but I guess the
    > answer will come later when I gain more experience.


    Example: I wrote a simple email signature randomiser¹ so
    that my email signatures are picked at random from a given
    pool (this email is to ruby-talk, so the one below is picked
    from the ones that are both technical and in English).

    I could store the signature database in an RDBMS, but that
    would be an overkill; all I need is a simple command-line
    program that reads the signatures from a file, selects those
    matching the requirements and then picks one from them.

    Without YAML I’d have to create a storage format that takes into
    account the signature’s body, author, subject, source and tags
    – with YAML I just serialise the signature object and I’m done.
    On the flip side, if I even find a typo in a signature, I can
    easily directly edit the YAML file² and be done with it.

    ¹ https://github.com/chastell/signore
    ² https://github.com/chastell/dotfiles/blob/master/.local/share/signore/signatures.yml

    — Piotr Szotkowski
    --
    The good thing about reinventing the wheel is that you can get a round one.
    [Douglas Crockford on JSON vs XML]
    Piotr Szotkowski, Mar 15, 2011
    #6
  7. On Tue, Mar 15, 2011 at 4:06 PM, Piotr Szotkowski <> w=
    rote:
    >
    > Without YAML I=92d have to create a storage format that takes into
    > account the signature=92s body, author, subject, source and tags
    > =96 with YAML I just serialise the signature object and I=92m done.
    > On the flip side, if I even find a typo in a signature, I can
    > easily directly edit the YAML file=B2 and be done with it.


    Neither of this is, of course, limited to YAML. YAML just has the
    benefit of being shipped with Ruby's standard library, thus it is
    ubiquitous within the Ruby world.

    That doesn't mean it's the best, simplest, or most efficient tool to
    store any ol' text in a programming language-compatible way (that's
    highly usage based, since sometimes performance matters, sometimes a
    small footprint in LOC matters, sometimes the time is limited to solve
    a problem, etc.).

    YAML's excellent at storing Ruby objects in a human-readable fashion,
    and importing them again Somewhere Else. It's excellent for
    configuration data, or simple jobs (I used a Ruby website templating
    engine using YAML for single pages/posts many moons ago), but falls
    apart when your data doesn't easily fit into the Ruby object mold
    (i.e. Hashes, Arrays, Strings, &c.).

    Which brings me to a nicely apropos email signature:

    > The good thing about reinventing the wheel is that you can get a round on=

    e.
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =

    =A0 =A0 [Douglas Crockford on JSON vs XML]

    The difference between JSON / YAML and XML is that that JSON and YAML
    do one thing, and do it well(-ish), while XML is extremely flexible.
    On the downside, that means JSON or YAML doesn't always work
    well(-ish), while XML is extremely flexible.

    --=20
    Phillip Gawlowski

    Though the folk I have met,
    (Ah, how soon!) they forget
    When I've moved on to some other place,
    There may be one or two,
    When I've played and passed through,
    Who'll remember my song or my face.
    Phillip Gawlowski, Mar 15, 2011
    #7
  8. Fily Salas

    Fily Salas Guest

    Fily Salas, Mar 15, 2011
    #8
  9. Fily Salas

    7stud -- Guest

    > I still don't know have a clear picture as
    > to where or how this will be used later


    When you are learning a language that really isn't important. The most
    important thing is understanding that you can do something and how to do
    it--not why it is important.

    Later, when you are writing a program, a light bulb may go off in your
    head, and you suddenly realize that some little thing you learned about
    in the past will work perfectly in your code. Or, it may be that such a
    light bulb may never go off, and the only way you will ever understand
    how to apply what you learned is by reading other people's code and
    seeing how they used YAML or some other feature you learned about in
    their code, and then you copy that idea in your code.

    You can think of YAML as a lightweight database if you want. If you
    don't need all the features of a full blown database, then you can use
    YAML instead. Or, you can think of YAML as a JSON or XML
    substitute--and then ask yourself why anyone would use JSON or XML.

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Mar 16, 2011
    #9
  10. Fily Salas

    Fily Salas Guest

    >You can think of YAML as a lightweight database if you want. If you
    >don't need all the features of a full blown database, then you can use
    >YAML instead. Or, you can think of YAML as a JSON or XML
    >substitute--and then ponder why anyone would need to use JSON or XML.


    Got it, thanks a lot!

    --
    Posted via http://www.ruby-forum.com/.
    Fily Salas, Mar 16, 2011
    #10
  11. Fily Salas

    7stud -- Guest

    The data stored in a mysql database does not have any style associated
    with it--the data is a string or a number or a date. You can of course
    retrieve data from a database, insert the data into an html page, and
    then add style to the data with css. Because YAML is also a database,
    you can do the same thing with data stored in a YAML file.

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Mar 17, 2011
    #11
  12. Fily Salas

    Fily Salas Guest

    Fily Salas, Mar 17, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RubyQuestions
    Replies:
    0
    Views:
    220
    RubyQuestions
    Dec 3, 2003
  2. Paul Battley

    YAML.dump/YAML.load bug

    Paul Battley, Aug 3, 2005, in forum: Ruby
    Replies:
    0
    Views:
    176
    Paul Battley
    Aug 3, 2005
  3. Eric Promislow
    Replies:
    4
    Views:
    206
    Eric Promislow
    Oct 31, 2006
  4. Joshua Choi

    yaml.rb and YAML "%" directives

    Joshua Choi, Jan 14, 2007, in forum: Ruby
    Replies:
    1
    Views:
    160
  5. Fransiscus Xaverius

    YAML Problem YAML::Object

    Fransiscus Xaverius, Dec 14, 2007, in forum: Ruby
    Replies:
    2
    Views:
    156
    Fransiscus Xaverius
    Dec 14, 2007
Loading...

Share This Page