{"action":"created","issue":{"url":"https://api.github.com/repos/opal/opal/issues/1028","labels_url":"https://api.github.com/repos/opal/opal/issues/1028/labels{/name}","comments_url":"https://api.github.com/repos/opal/opal/issues/1028/comments","events_url":"https://api.github.com/repos/opal/opal/issues/1028/events","html_url":"https://github.com/opal/opal/issues/1028","id":97139574,"number":1028,"title":"to_n isn't recursively called on arrays within a hash","user":{"login":"ashes999","id":18432,"avatar_url":"https://avatars.githubusercontent.com/u/18432?v=3","gravatar_id":"","url":"https://api.github.com/users/ashes999","html_url":"https://github.com/ashes999","followers_url":"https://api.github.com/users/ashes999/followers","following_url":"https://api.github.com/users/ashes999/following{/other_user}","gists_url":"https://api.github.com/users/ashes999/gists{/gist_id}","starred_url":"https://api.github.com/users/ashes999/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashes999/subscriptions","organizations_url":"https://api.github.com/users/ashes999/orgs","repos_url":"https://api.github.com/users/ashes999/repos","events_url":"https://api.github.com/users/ashes999/events{/privacy}","received_events_url":"https://api.github.com/users/ashes999/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-07-24T20:23:14Z","updated_at":"2015-07-25T07:33:52Z","closed_at":null,"body":"Originally found in #1024 . If you have a hash with arrays in it, `to_n` removes the Opal class instances from the hash, but not the arrays.\r\n\r\n```\r\nhash = {\r\n  :images => ['one.png', 'bg.png'],\r\n  :audio => ['point.ogg']\r\n}\r\n```\r\n\r\nCalling `hash.to_n` gives me a hash with only two properties; but the two arrays each have Opal class instances in it.\r\n\r\nThis broke CraftyJS, because they iterated over this hash like so:\r\n\r\n```\r\nfor (key in hash) {\r\n  for (item in hash[key])\r\n    if item.search('://') ... // error: method not found on OpalClass instance\r\n  }\r\n}\r\n```\r\n\r\nThe solution was to use `hasOwnProperty`, which [they did in CraftyJS](https://github.com/craftyjs/Crafty/pull/943). However, `to_n` should still probably be fixed so that it recursively \"native-ifies\" arrays within the hash.\r\n\r\nFor reference, a quote from #1027:\r\n\r\n> @elia:\r\n> yes, actually there's already a place for that kind of docs at http://opalrb.org/docs/ under \"API documentation\". It's unfortunate that for some reason Native isn't covered and we need to fix that.\r\n> As of the array to_n should call to_n on all its elements, if it doesn't let me know and we'll ask @meh to fix it :blush:\r\n\r\nThe easiest test, in my opinion, is to iterate using `fox item in hash[key]` and printing out the count. It should be 1-2 (the number of items in the array), not 300+.\r\n\r\nI have codified my example [here](http://opalrb.org/try/?code:hash%20%3D%20%7B%0A%20%20%3Aimages%20%3D%3E%20%5B%27one.png%27%2C%20%27bg.png%27%5D%2C%0A%20%20%3Aaudio%20%3D%3E%20%5B%27point.ogg%27%5D%0A%7D.to_n%0A%0A%60for%20(key%20in%20hash)%20%7B%20for%20(item%20in%20hash%5Bkey%5D)%20%7B%20console.log(%22Item%3A%20%22%20%2B%20item)%3B%20%7D%20%7D%60) (on opalrb.org/try). See for yourself."},"comment":{"url":"https://api.github.com/repos/opal/opal/issues/comments/124816418","html_url":"https://github.com/opal/opal/issues/1028#issuecomment-124816418","issue_url":"https://api.github.com/repos/opal/opal/issues/1028","id":124816418,"user":{"login":"elia","id":1051,"avatar_url":"https://avatars.githubusercontent.com/u/1051?v=3","gravatar_id":"","url":"https://api.github.com/users/elia","html_url":"https://github.com/elia","followers_url":"https://api.github.com/users/elia/followers","following_url":"https://api.github.com/users/elia/following{/other_user}","gists_url":"https://api.github.com/users/elia/gists{/gist_id}","starred_url":"https://api.github.com/users/elia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elia/subscriptions","organizations_url":"https://api.github.com/users/elia/orgs","repos_url":"https://api.github.com/users/elia/repos","events_url":"https://api.github.com/users/elia/events{/privacy}","received_events_url":"https://api.github.com/users/elia/received_events","type":"User","site_admin":false},"created_at":"2015-07-25T07:33:51Z","updated_at":"2015-07-25T07:33:51Z","body":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty\r\n\r\nObject.prototype.defineProperty seems the only way to mark a property as non-enumerable. But we should talk about browser support and performance before going down this route. "}}