[Nitro] Building objects from Strings

TRANS transfire at gmail.com
Wed Aug 9 05:55:56 EDT 2006


On 8/9/06, gabriele renzi <surrender_it at yahoo.it> wrote:
> Hi everyone,
>
> I have a somewhat working interface to change Settings from the
> administration UI, and I think it is a Good Thing(TM).
>
> But bat the moment it is slightly fragile, and uses a hacky way of
> getting the values from the user.
> Basically, I'm providigin small text fields for each "good" setting, and
> in the server side I'm assigning to the setting the value by
> transforming it in the right type.
>
> In code:
>
>   setting.value = setting.type.new(param[name])
>
> Now to the bad one: ruby does not have String constructors for many
> things, so the above is working fine for String and Numerics.
> I added the handling for Booleans as a simple
> def TrueClass.new(arg) arg=='true' end
> def FalseClass.new(arg) not TrueClass.new(arg) end
>
> A better solution could be using YAML.load on the given String.
> Someone has other ideas or could point me to something in facets (i.e.
> core/kernel/parse_literal) ?

Try:

  YAML.load( "--ruby/object:#{type} {}" )

Might be worth adding to Facets.

T.


More information about the Nitro-general mailing list