Inspecting Objects in Scripting
Examine the contents of a class / object is one of most useful and repetitive tasks that most of the coder do while debugging.
Here I try to focus on inspecting objects in scripting few of the major scripting languages that /me like.
Mainly JavaScript, Python, Ruby and Perl shall be covered in this article, feel free to suggest your ways to doing this.
Inspecting Objects in JavaScript :
Can easily be done with Object.keys()
from ES5 that returns an array of strings containing the names of all enumerable properties of that object, but it's not very useful as it wont let you know the difference between the keys; Difference based on their values, so something like below must be done
A simple example :
var info = { name: "Hemanth", url: "http://h3manth.com/", location : "Earth", get : function() {} }; function inspect(o) { var a = []; Object.getOwnPropertyNames(o).forEach(function (k) { a.push(Object.getOwnPropertyDescriptor(o, k)); }); return a; } inspect(info) [ Object configurable: true enumerable: true value: "http://h3manth.com/" writable: true <strong>proto</strong>: Object , Object configurable: true enumerable: true value: function () {} writable: true <strong>proto</strong>: Object , Object configurable: true enumerable: true value: "Hemanth" writable: true <strong>proto</strong>: Object , Object configurable: true enumerable: true value: "Earth" writable: true <strong>proto</strong>: Object ]
or To get an {} instead of []
var r = {}; Object.getOwnPropertyNames(o).forEach(function (k) { r[k] = Object.getOwnPropertyDescriptor(o, k); }); return r; }
P.S : It's my duty to thank Brendan from Mozilla for correcting and helping me to get better perspective :)
ES5 has plural Object.defineProperties for defining more than one property on an object, and of course Object.create that defines all the properties on a new object from a descriptor-map. But it doesn't have anything like this inspect.
Inspecting Objects in Python :
In Python objects are normally inspected with dir(obj)
but that is not satisfying and has the same problem as with JS's Object.keys()
.
So something better can be achieved with python's inspect
module.
import inspect # Let's inspect [] <list> print inspect.getmembers([])
[('__add__', <method-wrapper '__add__' of list object at 0x1004e52d8>),
('__class__', <type 'list'>),
('__contains__', <method-wrapper '__contains__' of list object at 0x1004e52d8>),
('__delattr__', <method-wrapper '__delattr__' of list object at 0x1004e52d8>),
('__delitem__', <method-wrapper '__delitem__' of list object at 0x1004e52d8>),
('__delslice__', <method-wrapper '__delslice__' of list object at 0x1004e52d8>),
('__doc__', "list() -> new list\nlist(sequence) -> new list initialized from sequence's items"),
......
Inspecting Objects in Ruby :
Well, Ruby, Object#inspect, p, awesome_print, Object#methods is what most use, but again not fully happy with them.
The drx gem just rocks here, well gem install drx
is a must here, as it's not bundled with ruby (yet).
require 'drx' 'hemanth'.see # Will give you a graphical rep of the obj
Well it's still debatable, as /me also like CLI; But 'drx' is amazing! It needs some tk deps, be sure you have them.
With our drx it would be like :
>> (String.instance_methods - Object.instance_methods).sort => ["%", "*", "+", "<", "<<", "<=", "<=>", ">", ">=", "[]", "[]=", "all?", "any?", "between?", "bytes", "bytesize", "capitalize", "capitalize!", "casecmp", "center", "chars", "chomp", "chomp!", "chop", "chop!", "collect", "concat", "count", "crypt", "cycle", "delete", "delete!", "detect", "downcase", "downcase!", "drop", "drop_while", "dump", "each", "each_byte", "each_char", "each_cons", "each_line", "each_slice", "each_with_index", "empty?", "end_with?", "entries", "enum_cons", "enum_slice", "enum_with_index", "find", "find_all", "find_index", "first", "grep", "group_by", "gsub", "gsub!", "hex", "include?", "index", "inject", "insert", "intern", "length", "lines", "ljust", "lstrip", "lstrip!", "map", "match", "max", "max_by", "member?", "min", "min_by", "minmax", "minmax_by", "next", "next!", "none?", "oct", "one?", "partition", "reduce", "reject", "replace", "reverse", "reverse!", "reverse_each", "rindex", "rjust", "rpartition", "rstrip", "rstrip!", "scan", "select", "size", "slice", "slice!", "sort", "sort_by", "split", "squeeze", "squeeze!", "start_with?", "strip", "strip!", "sub", "sub!", "succ", "succ!", "sum", "swapcase", "swapcase!", "take", "take_while", "to_f", "to_i", "to_str", "to_sym", "tr", "tr!", "tr_s", "tr_s!", "unpack", "upcase", "upcase!", "upto", "zip"]
Inspecting Objects in Perl :
Perl objects are blessed references and OO doesn't provide "attributes", but provides "methods"
use Class::Inspector; my $methods = Class::Inspector->methods('Class::Inspector','full'); print "Methods are [@$methods]\n"
Well that's it from me as of now, this comparative study was fun! Hope you liked it too :)
Recent blog posts
- watir-webdriver web inspector
- gem list to gemfile
- Packing ruby2.0 on debian.
- Made it into The Guinness Book!
- to_h in ruby 2.0
- Filter elements by pattern jQuery.
- Better HTML password fields for mobile ?
- Grayscale image when user offline
- nth-child CSS pseudo-class Christmas colors
- EventEmitter in nodejs