Skip to content

Instantly share code, notes, and snippets.

@roy
Created January 23, 2014 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roy/4f661f310616936ba606 to your computer and use it in GitHub Desktop.
Save roy/4f661f310616936ba606 to your computer and use it in GitHub Desktop.
def to_s
inspect
end
def inspect
%x{
var val, el, str, result = [];
for (var i = 0, length = self.length; i < length; i++) {
el = self[i];
if (el == document) { return '#<Element [document]'; }
str = "<" + el.tagName.toLowerCase();
if (val = el.id) str += (' id="' + val + '"');
if (val = el.className) str += (' class="' + val + '"');
result.push(str + '>');
}
return '#<Element [' + result.join(', ') + ']>';
}
end
@elia
Copy link

elia commented Jan 23, 2014

what about:

def inspect
  %x{
    var val, el, str, result = [];

    for (var i = 0, length = self.length; i < length; i++) {
      el  = self[i];
      if (!el.tagName) { return '#<Element ['+el.toString()+']'; }

      str = "<" + el.tagName.toLowerCase();

      if (val = el.id) str += (' id="' + val + '"');
      if (val = el.className) str += (' class="' + val + '"');

      result.push(str + '... >');
    }

    return '#<Element [' + result.join(', ') + ']>';
  }
end

alias to_s inspect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment