[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

defining a custom to_yaml method to inline hashes

Ara.T.Howard

5/11/2005 5:02:00 PM

5 Answers

Ara.T.Howard

5/11/2005 5:46:00 PM

0

why the lucky stiff

5/12/2005 12:07:00 AM

0

Ara.T.Howard@noaa.gov (Ara.T.Howard@noaa.gov) wrote:
> On Wed, 11 May 2005, Ara.T.Howard wrote:
> > # attempt to bludgeon to object into emitting in an inline fashion
> > class << inline_hash
> > def to_yaml(*a, &b)
> > '{ ' << map{|kv| kv.join(': ')}.join(', ') << ' }'
> > end
> > end

Hacking Syck 0.45 or less will be very involved. Consider that inline
collections must also contain inline hashes and inline arrays. And, as
your sample code demonstrated, some strings which get formatted in the
"folded" or "literal" block styles won't jive with inlines.

Syck 0.53 and up is a different story. You can easily emit inlines by
defining a `to_yaml_style' method.

>> require 'yaml'
>> a = {'time' => Time.now, 'jid' => 121, 'metric' => 'started'}
>> def a.to_yaml_style; :inline; end
>> y a
=> ---
{ time: 2005-05-10 08:18:22.523016, jid: 121, metric: started }

The new Syck emitter will ensure that everything inside the inline is
properly output. I still need to improve the pretty printer, but it
works.

> this seems o.k. - can anyone verify this is an o.k. approach?
>
> jib:~ > cat a.rb
> require 'yaml'
> class Hash
> def yaml_inline= bool
> unless defined? @__yaml_inline_meth
> @__yaml_inline_meth =
> lambda {|opts|
> YAML::quick_emit(id, opts) {|emitter|
> emitter << '{ ' << map{|kv| kv.join ': '}.join(', ') << ' }'
> }
> }
> class << self
> def to_yaml opts = {}
> @__yaml_inline ? @__yaml_inline_meth[ opts ] : super
> end
> end
> end
> @__yaml_inline = bool
> end
> end

This will work for very simple hashes, but will break down upon
encountering multiline strings or any kind of object, array, hash, etc.

While Syck 0.5x isn't ready for primetime, it's getting very close. I
could use help debugging and the time you spend debugging could yield a
stable Syck in the next two months.

_why


don

6/19/2008 8:13:00 AM

0

On Jun 18, 3:09 pm, don <donhar...@gmail.com> wrote:
> On Jun 18, 2:25 pm, Smallweed <Smallw...@discussions.microsoft.com>
> wrote:
>
> > Application.Run("workbook.xls!macro",arg1,arg2,etc)
>
> Thanks for reply but as mentioned in op this could be one of four
> workbooks (in fact more) so I would not know which workbook.xls to
> specify.  Can I use wildcards?
>
> Don

Any other thoughts please?

Don

don

6/19/2008 4:36:00 PM

0

On Jun 19, 1:14 pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> Maybe...(untested)
>
> Option Explicit
> Sub testme()
>
>     Dim ok As Boolean
>     Dim wkbk As Workbook
>
>     ok = False
>     For Each wkbk In Application.Workbooks
>       On Error Resume Next
>       Application.Run "'" & wkbk.Name & "'!macronamehere", "parm1"
>       If Err.Number <> 0 Then
>         Err.Clear
>       Else
>         ok = True
>         Exit For
>       End If
>       On Error GoTo 0
>     Next wkbk
>
>     If ok = False Then
>       MsgBox "Failed!"
>     Else
>       MsgBox "may it worked???"
>     End If
>
> End Sub
>
>
>
>
>
> don wrote:
>
> > On Jun 18, 3:09 pm, don <donhar...@gmail.com> wrote:
> > > On Jun 18, 2:25 pm, Smallweed <Smallw...@discussions.microsoft.com>
> > > wrote:
>
> > > > Application.Run("workbook.xls!macro",arg1,arg2,etc)
>
> > > Thanks for reply but as mentioned in op this could be one of four
> > > workbooks (in fact more) so I would not know which workbook.xls to
> > > specify.  Can I use wildcards?
>
> > > Don
>
> > Any other thoughts please?
>
> > Don
>
> --
>
> Dave Peterson- Hide quoted text -
>
> - Show quoted text -

Thanks for suggestion I'll post back to let you know if it worked.

Thanks again

don

6/23/2008 5:23:00 AM

0

On 19 Jun, 17:36, don <donhar...@gmail.com> wrote:
> On Jun 19, 1:14 pm, Dave Peterson <peter...@verizonXSPAM.net> wrote:
>
>
>
>
>
> > Maybe...(untested)
>
> > Option Explicit
> > Sub testme()
>
> >     Dim ok As Boolean
> >     Dim wkbk As Workbook
>
> >     ok = False
> >     For Each wkbk In Application.Workbooks
> >       On Error Resume Next
> >       Application.Run "'" & wkbk.Name & "'!macronamehere", "parm1"
> >       If Err.Number <> 0 Then
> >         Err.Clear
> >       Else
> >         ok = True
> >         Exit For
> >       End If
> >       On Error GoTo 0
> >     Next wkbk
>
> >     If ok = False Then
> >       MsgBox "Failed!"
> >     Else
> >       MsgBox "may it worked???"
> >     End If
>
> > End Sub
>
> > don wrote:
>
> > > On Jun 18, 3:09 pm, don <donhar...@gmail.com> wrote:
> > > > On Jun 18, 2:25 pm, Smallweed <Smallw...@discussions.microsoft.com>
> > > > wrote:
>
> > > > > Application.Run("workbook.xls!macro",arg1,arg2,etc)
>
> > > > Thanks for reply but as mentioned in op this could be one of four
> > > > workbooks (in fact more) so I would not know which workbook.xls to
> > > > specify.  Can I use wildcards?
>
> > > > Don
>
> > > Any other thoughts please?
>
> > > Don
>
> > --
>
> > Dave Peterson- Hide quoted text -
>
> > - Show quoted text -
>
> Thanks for suggestion I'll post back to let you know if it worked.
>
> Thanks again- Hide quoted text -
>
> - Show quoted text -

Yes worked fine thanks

Don