I actually mis-typed the logic from Template#evaluate - what
Template#evaluate uses is not:
return before + String(ctx);
but:
return before + String.interpret(ctx);
where String.interpret() is simply:
return (value == null) ? '' : String(value);
The problem is that the toString() operation called on an object of
type Function returns the literal string source of the function.
I'm not sure that I see where checking the type is limiting - if it is
a function, apply it with the current object; if it is not send it
through the usual interpretation. Looking at the patches referred to
earlier in the thread, it looks like handling things in that manner is
pretty straightforward, and has a fair amount of utility especially
when nesting or chaining functions..
On Mar 5, 2:34 pm, kangax <[EMAIL PROTECTED]> wrote:
> Interesting.
> Actually String( ) already acts as a sort of a delegator by invoking
> toString( ) on whatever you pass into it. It's actually quite flexible
> - we can *redefine* toString on any object and make it return anything
> we want. Checking for type (as you propose) just seems a little too
> limiting to me.
>
> Take a look at how easy the implementation is:
> (by redefining native "toString" on instance methods to return curried
> versions of themselves)
>
> ...
> var myObj = new MyClass('A', 'B', 'C');
>
> $w(' first second third ').each(function(m) {
> myObj[m].toString = Prototype.K.curry(myObj[m]());
>
> })
>
> var myOut = 'First: #{first}, Second: #{second}, Third:
> #{third}'.interpolate(myObj);
>
> myOut; // => "First: A, Second: B, Third: C"
>
> The only limitatation I see here is that we need to know a list of
> methods to augment. On the other hand, it is possible to change
> Class.create to do all of this in a more generic way.
>
> Best,
> kangax
>
> On Mar 5, 1:12 pm, "J. Gregory Wright" <[EMAIL PROTECTED]> wrote:
>
> > If I have [admittedly simple] code as follows:
>
> > MyClass = Class.create({
> > _a: false,
>
> > _ b: false,
>
> > _c: false,
>
> > initialize: function(a, b, c) {
> > this._a = a;
> > this._b = b;
> > this._c = c;
> > },
>
> > first: function(){
> > return this._a;
> > },
>
> > second: function(){
> > return this._b;
> > },
>
> > third: function(){
> > return this._c;
> > }
>
> > });
>
> > var myObj = new MyClass('A','B','C');
> > var myTmpl = new Template('First: #{first}, Second: #{second}, Third:
> > #{third}');
> > var myOut = myTmpl.evaluate(myObj);
>
> > Then the string value placed in myOut will not be:
>
> > "First: A, Second: B, Third: C"
>
> > but rather will be:
>
> > "First: function(){
> > return this._a;
> > }, Second: function(){
> > return this._b;
> > }, Third:function(){
> > return this._c;
> > }"
>
> > Based on the code for Template#evaluate, it would seem that the line:
>
> > return before + String(ctx);
>
> > could be replaced by:
>
> > return before + (Object.isFunction(ctx)) ? ctx.apply(object) :
> > String(ctx);
>
> > and produce [the/my] desired effect of rendering the return value from
> > the function instead of the function literal. Is there a reason why
> > this would not be workable, or should not be done?
>
> > Thanks in advance,
> > G
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---