REXML : add text to element without temp variable?

Discussion in 'Ruby' started by CD Baby, Jul 18, 2004.

  1. CD Baby

    CD Baby Guest

    Say I'm trying to make this XML:
    <music>
    <artist>Pink Floyd</artist>
    <album>Dark Side of the Moon</album>
    </music>

    Right now I'm doing this:
    doc = Document.new
    music = doc.add_element 'music'
    artist = music.add_element 'artist'
    artist.text = 'Pink Floyd'
    album = music.add_element 'album'
    album.text = 'Dark Side of the Moon'

    QUESTION:
    Why create variables for artist and album?
    Shouldn't there be a way to add the text directly, without creating an
    in-between (and never used again) variable?

    Like this:
    music.add_element('album').text = 'Dark Side of the Moon'

    or this:
    music.add_element('album').text << Text.new('Dark Side of the Moon')

    Has anyone found a shortcut like this?
     
    CD Baby, Jul 18, 2004
    #1
    1. Advertising

  2. --zYM0uCDKw75PZbzx
    Content-Type: text/plain; charset=iso-8859-1
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    On Sun, Jul 18, 2004 at 12:09:25PM +0900, CD Baby wrote:
    > Say I'm trying to make this XML:
    > <music>
    > <artist>Pink Floyd</artist>
    > <album>Dark Side of the Moon</album>
    > </music>
    >=20
    > Right now I'm doing this:
    > doc =3D Document.new
    > music =3D doc.add_element 'music'
    > artist =3D music.add_element 'artist'
    > artist.text =3D 'Pink Floyd'
    > album =3D music.add_element 'album'
    > album.text =3D 'Dark Side of the Moon'
    >=20
    > QUESTION:
    > Why create variables for artist and album?
    > Shouldn't there be a way to add the text directly, without creating an
    > in-between (and never used again) variable?
    >=20
    > Like this:
    > music.add_element('album').text =3D 'Dark Side of the Moon'
    >=20
    > or this:
    > music.add_element('album').text << Text.new('Dark Side of the Moon')
    >=20
    > Has anyone found a shortcut like this?
    >=20


    doc =3D Document.new
    music =3D doc.add_element("music")
    music << Element.new("artist").add_text("Pink Floyd")
    music << Element.new("album").add_text("Dark side of the moon")

    //Anders

    --=20
    =2E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
    =2E Anders Engstr=F6m
    =2E http://www.gnejs.net PGP-Key: ED010E7F
    =2E [Your mind is like an umbrella. It doesn't work unless you open it.] =
    =20


    --zYM0uCDKw75PZbzx
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: Digital signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (GNU/Linux)

    iD8DBQFA+kOeuNLLbe0BDn8RAioLAJoClSDPSvFw/Tde4nIDZscscBOeQwCcDSp0
    EETjDe4R+sch+XD4UyCjMb4=
    =e4H2
    -----END PGP SIGNATURE-----

    --zYM0uCDKw75PZbzx--
     
    Anders Engström, Jul 18, 2004
    #2
    1. Advertising

  3. CD Baby

    CD Baby Guest

    Thanks everyone for your help.
    You're right, this works great:

    doc2 = REXML::Document.new
    music = doc2.add_element 'music'
    music.add_element('artist').text = 'Pink Floyd'
    music.add_element('album').text = 'Dark Side of the Moon'

    Not sure why it wasn't working last night, except maybe I needed some
    sleep. :)
     
    CD Baby, Jul 18, 2004
    #3
  4. On Monday, July 19, 2004, 1:32:55 AM, CD wrote:

    > Thanks everyone for your help.
    > You're right, this works great:


    > doc2 = REXML::Document.new
    > music = doc2.add_element 'music'
    > music.add_element('artist').text = 'Pink Floyd'
    > music.add_element('album').text = 'Dark Side of the Moon'


    > Not sure why it wasn't working last night, except maybe I needed some
    > sleep. :)


    Or some more energetic music!? :)

    Gavin
     
    Gavin Sinclair, Jul 18, 2004
    #4
    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. Replies:
    2
    Views:
    582
    Mark P
    May 9, 2005
  2. HANM
    Replies:
    2
    Views:
    734
    Joseph Kesselman
    Jan 29, 2008
  3. Phlip
    Replies:
    0
    Views:
    149
    Phlip
    Jan 15, 2008
  4. A. Farber
    Replies:
    3
    Views:
    274
    Ben Morrow
    Mar 3, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    269
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page