"illocutionist" <gltewalt / yahoo.com> schrieb im Newsbeitrag
news:a081657b.0406051558.3b9c7349 / posting.google.com...
> "Sam Sungshik Kong" <ssk / chol.nospam.net> wrote in message
news:<rMmvc.63963$Wf7.46733 / newssvr29.news.prodigy.com>...
> > Thanks to all who replied to my question.
> >
> > I want to mention one thing.
> > One drawback of ruby's #{name} mechanism is that the variable 'name'
must
> > exist before I setup the format and another drawback is that when I
setup
> > the format the result string is already made (returned) and cannot be
> > delayed (if I'm correct...).
> > Sometimes I want to make the format first, especially when I write
scripts
> > for web pages with python (now ruby :-)).
> >
> > In python for ASP, for instance, I frequently write the following style
> > code.
> >
> > html = """
> >     <tr>
> >         <td>%(name)s</td>
> >         <td>%(age)d</td>
> >     </tr>
> > """
> > di = {
> >     "name": get_name_from_db(),
> >     "age": get_age_from_db()
> > }
> > Response.Write(html % di)
> >
> > Well, I may change the sequence (get the values first and setup the
> > formatting string).
> > However, in the above case, formatting is outer bound and the values are
> > details.
> > My brain feels more comfortable in this way...;^)
> >
> > Sam
> >
> >
> >
>
> what about?...
>
> require 'ostruct'
> di = OpenStruct.new
>
> html = """
>   <tr>
>      <td>%s</td>
>      <td>%d</td>
>   </tr>
> """
>
> Response.Write(html % [di.name = "Sam", di.age = 34])
>
> Or, something similar to that.

Disadvantage of this is that you need to know the order in which %
expressions appear in the string.  Not very handy for larger web pages.  The
lookup idiom (i.e. placing identifiers in the page that are looked up later)
can be handled far better especially during a longer lifetime of a project.

Regards

    robert