Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

class << self #14

Closed
wants to merge 1 commit into from

2 participants

@mihu

Hi,

It seems that fog currently doesn't distinguish between instance methods and class methods defined in class << self. Example code

class Test
  class << self
    def test
      puts 'example'
    end
  end

  def test                                                                                                                              
      puts 'example'                                                                                                                    
  end                                                                                                                                   
end 

This gives me only the last method

     7.5: flog total
     3.8: flog/method average

     5.0: Test#none
     2.5: Test#test                        example.rb:9

with the patch I get this

     7.5: flog total
     2.5: flog/method average

     5.0: Test#none
     1.5: Test::test                       example.rb:4
     1.0: Test#test                        example.rb:9

The patch is a quick hack which works for me, don't exptect it to be pulled. Any ideas on how to make it less dirty?

@zenspider
Owner

This looks good... but it needs a test.

@zenspider
Owner

Hrm... @sclass should probably be a stack. See #in_klass

@zenspider zenspider was assigned
@mihu

well, the stack was my initial thought, but I'm totaly not sure how to handle something more complicated and frankly speaking, I don't even know how more complicated would look like. The only other thing coming to my mind is something like this

class << Time
  def something
  end
end

which posted patch doesn't handle properly, but that still doesn't require stack.

@zenspider
Owner

Yeah. I'm drawing a blank on when it'd occur... but I've parsed all of rubygems and know that if it can, it will...

Still need a test.

@zenspider
Owner

Awesome. Works for me. I'll let someone else report nesting issues if/when they come up. Thanks!

@zenspider zenspider closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 27, 2012
  1. [Add] dummy sclass defn handling

    Michal Humpula authored
This page is out of date. Refresh to see the latest.
View
BIN  lib/.flog.rb.swp
Binary file not shown
View
8 lib/flog.rb
@@ -79,7 +79,7 @@ class Flog < SexpProcessor
@@no_method = :none
attr_accessor :multiplier
- attr_reader :calls, :option, :class_stack, :method_stack, :mass
+ attr_reader :calls, :option, :class_stack, :method_stack, :mass, :sclass
attr_reader :method_locations
def self.plugins
@@ -324,6 +324,7 @@ def in_method(name, file, line)
def initialize option = {}
super()
@option = option
+ @sclass = nil
@class_stack = []
@method_stack = []
@method_locations = {}
@@ -621,7 +622,8 @@ def process_dasgn_curr(exp) # FIX: remove
alias :process_lasgn :process_dasgn_curr
def process_defn(exp)
- in_method exp.shift, exp.file, exp.line do
+ name = @sclass ? "::#{exp.shift}" : exp.shift
+ in_method name, exp.file, exp.line do
process_until_empty exp
end
s()
@@ -736,10 +738,12 @@ def process_module(exp)
end
def process_sclass(exp)
+ @sclass = true
penalize_by 0.5 do
process exp.shift # recv
process_until_empty exp
end
+ @sclass = nil
add_to_score :sclass
s()
View
BIN  test/.test_flog.rb.swp
Binary file not shown
View
15 test/test_flog.rb
@@ -399,6 +399,21 @@ def test_process_defn
util_process sexp, 0.275, :lit_fixnum => 0.275
end
+ def test_process_defn_in_self
+ sexp = s(:sclass, s(:self),
+ s(:defn, :x,
+ s(:args, :y),
+ s(:lit, 42)))
+
+ setup
+ @flog.process sexp
+
+ exp = {'main::x' => {:lit_fixnum => 0.375}, 'main#none' => {:sclass => 5.0}}
+ assert_equal exp, @flog.calls
+
+ assert_in_delta 5.375, @flog.total
+ end
+
def test_process_defs
@meth = "::x" # HACK, I don't like this
Something went wrong with that request. Please try again.