How to count instance variable, Methods, Classes and comment lines in Ruby -
how count instance variable, methods, classes , comment lines in ruby.
class admincontroller < applicationcontroller include applicationhelper ssl_required :edit_account def index @admin_projects = @admin.projects.find(:all, :conditions => ["project_completed=?",0]) total_tasks = find_project_and_completed_task_before_and_after_date(@admin_projects) @project_task_bar_chart = googlechart::barchart.new('450x150', "", :horizontal, true) @project_task_bar_chart.data_encoding = :text max_cate_task = total_tasks[2].sort.last @project_task_bar_chart.data "complete before due date", total_tasks[1], '67ce04' @project_task_bar_chart.data "complete after due date", total_tasks[0], 'ff0000' end #if request.xhr? #@project = @admin.projects.find(:first,:conditions => ["projects.id = ?",params[:project_id].to_i])
the question, "how many?", answered sizes of arrays referred below, except number of classes.
instance variables
c.new.instance_variables
returns array of instance of class c
's instance variables.
c.instance_variables
returns array of class c
's instance variables (generally referred class instance variables).
for example:
class @a = 'cat' def initialize @b = 'dog' @c = 'pig' end end a.instance_variables #=> [:@a] a.new.instance_variables #=> [:@b, :@c]
instance methods
c.instance_methods
(or c.new.methods
) returns array of c
's instance methods, including inherited ones. c
can module not class.
c.instance_methods(false)
(or c.new.methods(false)
) returns array of c
's instance methods defined on class c
. c
can module not class. modules not inherit methods, if c
module, c.instance_methods == c.instance_methods(false)
.
the ownership of 1 of class c
's instance methods m
can obtains c.instance_method(:m).owner
.
class methods
c.methods
, c.methods(false)
return class c
's class methods. case when c
module not class.
the ownership of 1 of class c
's class methods m
can obtained c.method(:m).owner
.
classes
execute
gc.start
to force immediate garbage collection, then:
objectspace.each_object(class).to_a.size
or
objectspace.count_objects[:t_class]
if singleton classes included.
these of course counts both built-in , user-defined classes. understand there
my @frederickcheung clarifying this.
comments
determining numbers of comment lines requires clarification , different type of question foregoing. these reasons suggest made separate question (clarified appropriately). note comment may on line itself, @ end of line preceded code or between begin=
, end=
. moreover, character #
not precede comment (e.g., s = "#{cat}" # assign name of cat s
. counting comments non-trivial task.
Comments
Post a Comment