[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-28 Thread RobG



On Mar 28, 2:03 pm, Ryan Gahl [EMAIL PROTECTED] wrote:
 I believe, though, that these proposed solutions will be equally deemed by
 the OP's company as too much work... otherwise they would just fix their
 for..in loops to not be for..in loops, right?

Not necessarily, only the OP can answer that.  It might be that adding
a simple test to every for..in loop is easier than changing the arrays
to objects with an array property, which would require a change to
every for..in loop anyway.

Or it might not.

Perhaps the authors of Prototype.js can consider not extending
Array.prototype and creating their own array object that is extended,
so that $A() returns an extended array and the built-in constructor
does not.


--
Rob
--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-28 Thread Ryan Gahl
Meh... I see how this is an issue where people are using for..in
incorrectly, but I don't think the authors of Prototype should try changing
anything now to appease some (yes) poorly written legacy code. That would
force many people to have to $A() wrap their Arrays where they don't have to
right now, just because some legacy code can't be bothered to be fixed up :)
-- sorry Rob, -1 from me.

Anyway, I think the OP has more or less moved on (not joining in the fun
debate anymore). Ah, well, se la vie.

On 3/28/08, RobG [EMAIL PROTECTED] wrote:




 On Mar 28, 2:03 pm, Ryan Gahl [EMAIL PROTECTED] wrote:
  I believe, though, that these proposed solutions will be equally deemed
 by
  the OP's company as too much work... otherwise they would just fix
 their
  for..in loops to not be for..in loops, right?


 Not necessarily, only the OP can answer that.  It might be that adding
 a simple test to every for..in loop is easier than changing the arrays
 to objects with an array property, which would require a change to
 every for..in loop anyway.

 Or it might not.

 Perhaps the authors of Prototype.js can consider not extending
 Array.prototype and creating their own array object that is extended,
 so that $A() returns an extended array and the built-in constructor
 does not.



 --
 Rob
 



-- 
Ryan Gahl
Manager, Senior Software Engineer
Nth Penguin, LLC
http://www.nthpenguin.com
--
WebWidgetry.com / MashupStudio.com
Future Home of the World's First Complete Web Platform
--
Inquire: 1-920-574-2218
Blog: http://www.someElement.com
LinkedIn Profile: http://www.linkedin.com/in/ryangahl

--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-27 Thread Ryan Gahl
Yes, and the thing you need to do to avoid this is not use for..in loops
with an Array. Using Array that way is wrong (sorry to be so blunt, not
trying to be rude). If you're doing for..in, use an Object. Objects are
after all associative arrays in reality. Iterating Array objects using
for..in is basically wasting an Array. You're using the fact that it is also
an Object, but are not using the special features of Array. You should
really be .push()'ing objects into your array (not setting them via
myArray[someKey] = foo)... and iterating them using the numeric indexing
(or prototype's .each() if you want to have true block level scoping for
each iteration pass)

To summarize: don't use for..in to iterate and Array instance. Period.
That's the answer. Bar none. It's basically the root of all evil... not
gonna lie.

:)

On 3/27/08, Muthu [EMAIL PROTECTED] wrote:


 I have a small module that I am developing using Prototype. I have
 javascript Array's declared in my existing app and the we access the
 elements using for...in - for(x in myArray...).

 When I include my new module with Prototype to my existing app, all
 the for..in statements for the Array are getting the functions from
 the prototype.js as elements in it. Is there something I need to do to
 avoid this?

 Thanks,
 Muthiah.
 



-- 
Ryan Gahl
Manager, Senior Software Engineer
Nth Penguin, LLC
http://www.nthpenguin.com
--
WebWidgetry.com / MashupStudio.com
Future Home of the World's First Complete Web Platform
--
Inquire: 1-920-574-2218
Blog: http://www.someElement.com
LinkedIn Profile: http://www.linkedin.com/in/ryangahl

--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-27 Thread Muthu

Thanks for the fast response. But the issue here is that the existing
app is very big and complicate. Modifying all the array's to objects
and testing all the functionalities is going to kill our time. Believe
me, if I am going to propose this solution, first thing that will come
up is that they will not allow our module to use prototype.

Having said this, is it possible to do something within my module?

Thanks,
Muthu.

On Mar 27, 3:50 pm, Ryan Gahl [EMAIL PROTECTED] wrote:
 Yes, and the thing you need to do to avoid this is not use for..in loops
 with an Array. Using Array that way is wrong (sorry to be so blunt, not
 trying to be rude). If you're doing for..in, use an Object. Objects are
 after all associative arrays in reality. Iterating Array objects using
 for..in is basically wasting an Array. You're using the fact that it is also
 an Object, but are not using the special features of Array. You should
 really be .push()'ing objects into your array (not setting them via
 myArray[someKey] = foo)... and iterating them using the numeric indexing
 (or prototype's .each() if you want to have true block level scoping for
 each iteration pass)

 To summarize: don't use for..in to iterate and Array instance. Period.
 That's the answer. Bar none. It's basically the root of all evil... not
 gonna lie.

 :)

 On 3/27/08, Muthu [EMAIL PROTECTED] wrote:



  I have a small module that I am developing using Prototype. I have
  javascript Array's declared in my existing app and the we access the
  elements using for...in - for(x in myArray...).

  When I include my new module with Prototype to my existing app, all
  the for..in statements for the Array are getting the functions from
  the prototype.js as elements in it. Is there something I need to do to
  avoid this?

  Thanks,
  Muthiah.

 --
 Ryan Gahl
 Manager, Senior Software Engineer
 Nth Penguin, LLChttp://www.nthpenguin.com
 --
 WebWidgetry.com / MashupStudio.com
 Future Home of the World's First Complete Web Platform
 --
 Inquire: 1-920-574-2218
 Blog:http://www.someElement.com
 LinkedIn Profile:http://www.linkedin.com/in/ryangahl
--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-27 Thread Dan Dorman

On Thu, Mar 27, 2008 at 4:44 PM, Muthu [EMAIL PROTECTED] wrote:

  I have a small module that I am developing using Prototype. I have
  javascript Array's declared in my existing app and the we access the
  elements using for...in - for(x in myArray...).

Like Ryan said, don't use for ... in.

In prototype, you can loop over the items in an array like so:

my_array_variable.each(function(element) {
  // do cool stuff with the element here
});

And that's just the tip of the iceberg of stuff you can do with arrays
and other enumerable data types in Prototype. Check out the API docs
for stuff like map() and invoke().

:Dan

--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-27 Thread Ryan Gahl
Well, perhaps someone else knows a trick or two for this. I guess my
suggestion, if it's that big of an issue, is similar to your peers: don't
use prototype. The alternative to that might include some porting of
prototype on your end so that it doesn't add methods to the Array's
prototype. But that's problematic.

Just so you are aware, you should probably advise your company against use
almost any 3rd part libraries then (not just prototype). Array is extended
quite frequently, and if you can't spend the time to fix your legacy
application you will probably find this issue almost no matter what.

--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-27 Thread kangax

ProtoSafe could be another option http://www.protolific.net/

- kangax

On Mar 27, 7:12 pm, Ryan Gahl [EMAIL PROTECTED] wrote:
 Well, perhaps someone else knows a trick or two for this. I guess my
 suggestion, if it's that big of an issue, is similar to your peers: don't
 use prototype. The alternative to that might include some porting of
 prototype on your end so that it doesn't add methods to the Array's
 prototype. But that's problematic.

 Just so you are aware, you should probably advise your company against use
 almost any 3rd part libraries then (not just prototype). Array is extended
 quite frequently, and if you can't spend the time to fix your legacy
 application you will probably find this issue almost no matter what.
--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-27 Thread Ryan Gahl
On 3/27/08, kangax [EMAIL PROTECTED] wrote:


 ProtoSafe could be another option http://www.protolific.net/



Community for the win!

--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-27 Thread RobG


On Mar 28, 8:44 am, Muthu [EMAIL PROTECTED] wrote:
 I have a small module that I am developing using Prototype. I have
 javascript Array's declared in my existing app and the we access the
 elements using for...in - for(x in myArray...).

 When I include my new module with Prototype to my existing app, all
 the for..in statements for the Array are getting the functions from
 the prototype.js as elements in it. Is there something I need to do to
 avoid this?

You can test each property with hasOwnProperty:

  for (var p in obj) {
if (obj.hasOwnProperty(p)) {
  // p is a propery of obj, not its prototype chain
}
  }

--
Rob

--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-27 Thread kangax

Isn't hasOwnProperty missing in Safari  2.0.2 ?

- kangax

On Mar 27, 9:25 pm, RobG [EMAIL PROTECTED] wrote:
 On Mar 28, 8:44 am, Muthu [EMAIL PROTECTED] wrote:

  I have a small module that I am developing using Prototype. I have
  javascript Array's declared in my existing app and the we access the
  elements using for...in - for(x in myArray...).

  When I include my new module with Prototype to my existing app, all
  the for..in statements for the Array are getting the functions from
  the prototype.js as elements in it. Is there something I need to do to
  avoid this?

 You can test each property with hasOwnProperty:

   for (var p in obj) {
 if (obj.hasOwnProperty(p)) {
   // p is a propery of obj, not its prototype chain
 }
   }

 --
 Rob
--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-27 Thread RobG



On Mar 28, 11:32 am, kangax [EMAIL PROTECTED] wrote:
 Isn't hasOwnProperty missing in Safari  2.0.2 ?

Apparently, propertyIsEnumerable is an alternative but I think it's
missing from those versions too.

I probably should also mention that the only robust way to use for..in
is to implement object creation using a scheme where you know exactly
the propreties to include or ignore.  You might like to try this one
in Firefox and IE:

var t = {};
alert(
  t.hasOwnProperty('__proto__') + '\n' +
  t.hasOwnProperty('__parent__') + '\n' +
  '');

Perahps an alternative is to see if the property exists in an
unextended $A:

  var testArray = $A();
  for (var p in obj) {
if (!(p in testArray)) {
  // p is not a property of $A
}
  }

Another strategy is to modify Prototype.js so that all methods added
to Array.prototype also have a dontEnum property added that is set to
true, something like:

  Object.extend(Array.prototype, {
_each: function(iterator) {
   ...
},
...
toJSON: function() {
   ...
}
  });

  for (var p in Array.prototype) {
Array.prototype[p].dontEnum = true;
  }

Which assumes all the additions are objects, which seems OK, and
then...

  for (var p in obj) {
if (!obj[p].dontEnum) {
  ...
}
  }

Noting that you can't actually make any property DontEnum using native
code.  The last approach is untested, feel free to criticise.


--
Rob
--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---



[Rails-spinoffs] Re: for..in returning Prototype functions

2008-03-27 Thread Ryan Gahl
I believe, though, that these proposed solutions will be equally deemed by
the OP's company as too much work... otherwise they would just fix their
for..in loops to not be for..in loops, right?



On 3/27/08, RobG [EMAIL PROTECTED] wrote:




 On Mar 28, 11:32 am, kangax [EMAIL PROTECTED] wrote:
  Isn't hasOwnProperty missing in Safari  2.0.2 ?


 Apparently, propertyIsEnumerable is an alternative but I think it's
 missing from those versions too.

 I probably should also mention that the only robust way to use for..in
 is to implement object creation using a scheme where you know exactly
 the propreties to include or ignore.  You might like to try this one
 in Firefox and IE:

 var t = {};
 alert(
   t.hasOwnProperty('__proto__') + '\n' +
   t.hasOwnProperty('__parent__') + '\n' +
   '');

 Perahps an alternative is to see if the property exists in an
 unextended $A:

   var testArray = $A();

   for (var p in obj) {

 if (!(p in testArray)) {
   // p is not a property of $A
 }
   }

 Another strategy is to modify Prototype.js so that all methods added
 to Array.prototype also have a dontEnum property added that is set to
 true, something like:

   Object.extend(Array.prototype, {
 _each: function(iterator) {
...
 },
 ...
 toJSON: function() {
...
 }
   });

   for (var p in Array.prototype) {
 Array.prototype[p].dontEnum = true;
   }

 Which assumes all the additions are objects, which seems OK, and
 then...


   for (var p in obj) {

 if (!obj[p].dontEnum) {
   ...
 }
   }

 Noting that you can't actually make any property DontEnum using native
 code.  The last approach is untested, feel free to criticise.



 --
 Rob
 



-- 
Ryan Gahl
Manager, Senior Software Engineer
Nth Penguin, LLC
http://www.nthpenguin.com
--
WebWidgetry.com / MashupStudio.com
Future Home of the World's First Complete Web Platform
--
Inquire: 1-920-574-2218
Blog: http://www.someElement.com
LinkedIn Profile: http://www.linkedin.com/in/ryangahl

--~--~-~--~~~---~--~~
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 rubyonrails-spinoffs@googlegroups.com
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
-~--~~~~--~~--~--~---