Podrška #18027
ZatvorenTake a Looksee at how a Ruby Object got its Methods
100%
Izmjenjeno od Ernad Husremović prije više od 15 godina
One property of the Ruby object model and object oriented programming in general is that a subclass of an object automatically inherits all of the methods of its superclass. Classes can further expand the number of methods available by mixing in a Module, or several.
Because of mixins and subclassing even a class that has declared just a few methods can actually have hundreds of methods on it. In Ruby, all classes subclass Object by default which declares a hefty 45 methods, guaranteeing you to have at least that many. Out of the box in 1.8.7, a Ruby String object has 176 instance methods. If you are programming on top of the Rails framework, ActiveSupport adds 98 methods bringing the total to 274!
On numerous occasions I have needed to see what methods are available on an object I am working with I will type the following in irb.
myobject.methods - Object.instance_methods
This prints out a large array of instance methods with the methods inherited from Object removed from the list. This is useful but what if the object I am working with mixed in several modules and I am left with a list of over a hundred methods? It would be great to view which Class or Module each method came from. Well, actually there's a gem for that.™
Looksee
Looksee is a new gem by George Ogata that examines the method lookup path of any object. To use it add require 'looksee/shortcuts' to your ~/.irbrc. This will add a lp ("lookup path") method to your irb environment. When passed an object lp prints out a colored display showing where each of an object's methods lives.
looksee output for a string object- public methods are show in green
- protected methods are show in yellow
- private methods are show in red
- overwritten methods are show in gray
Go ahead and install Looksee and play around with it for a moment. Run lp on a String in vanilla irb and then open script/console in a Rails project and do the same thing. It is quite eye-opening to see the additions that the Rails framework makes.
Izmjenjeno od Ernad Husremović prije više od 15 godina
bringout@nmraka-2:~$ cat ~/.irbrc
require 'rubygems' require 'looksee/shortcuts'
Izmjenjeno od Ernad Husremović prije više od 15 godina
prije toga:
bringout@nmraka-2:~$ sudo gem install oggy-looksee
Izmjenjeno od Ernad Husremović prije više od 15 godina
irb(main):004:0> a=[1,"a",[2,3]]
irb(main):006:0> lp a
=> Array & combination fetch last reverse! take_while * compact fill length reverse_each to_a + compact! find_index map rindex to_ary - concat first map! select to_s << count flatten nitems shift to_yaml <=> cycle flatten! pack shuffle transpose == delete frozen? permutation shuffle! uniq [] delete_at hash pop size uniq! []= delete_if include? product slice unshift assoc drop index push slice! values_at at drop_while indexes rassoc sort yaml_initialize choice each indices reject sort! zip clear each_index insert reject! taguri | collect empty? inspect replace taguri= collect! eql? join reverse take Enumerable all? each_cons find_all max one? take any? each_slice find_index max_by partition take_while collect each_with_index first member? reduce to_a count entries grep min reject zip cycle enum_cons group_by min_by reverse_each detect enum_slice include? minmax select drop enum_with_index inject minmax_by sort drop_while find map none? sort_by Object dump_lookup_path taguri to_yaml to_yaml_style lookup_path taguri= to_yaml_properties Kernel == extend instance_variables singleton_methods === freeze is_a? taint =~ frozen? kind_of? tainted? __id__ hash method tap __send__ id methods to_a class inspect nil? to_enum clone instance_eval object_id to_s display instance_exec private_methods type dup instance_of? protected_methods untaint enum_for instance_variable_defined? public_methods eql? instance_variable_get respond_to? equal? instance_variable_set send
Izmjenjeno od Ernad Husremović prije više od 15 godina
- Status promijenjeno iz Novo u Zatvoreno
- % završeno promijenjeno iz 0 u 100