Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Foundations Blocks Examples

Ruby Foundations > Blocks > EC: comparison of Fixnum with nil failed (ArgumentError)

Hi there,

I'm new to ruby, and haven't written any code in a long time. I have been working through the extra credit task for the Ruby Foundations > Blocks section and have become a little stuck.

On trying to run my code I'm getting the following error:

blocks_4_examples_4.rb:17:in `<': comparison of Fixnum with nil failed (ArgumentError)
    from blocks_4_examples_4.rb:17:in `go'
    from blocks_4_examples_4.rb:41:in `<main>'

I'm trying to run the benchmarker based on the number of times the user specifies.

When I replace @run_count (within the go method) with an int I don't get the error, so I assume the issue is related to @run_count.

Here's my code, hopefully someone can shed some light on where I'm going wrong. Thanks for taking a look regardless!

class SimpleBenchmarker

    def initialize
        puts "How many iterations of the benchmarker would you like to run? "
        @run_count = gets.to_i
        if @run_count <= 10
            puts "Initiating..."
        else
            puts "Please enter an integer value less than 10."
            initialize
        end
    end

    def self.go(how_many=1, &block)
        results = Array.new
        i = 0
        while i < @run_count
            puts "---------- Benchmarking Started ----------"
            start_time = Time.now
            puts "Start Time: \t#{start_time}\n\n"
            how_many.times do |a|
                print "."
                block.call
            end
            print "\n\n"
            end_time = Time.now
            puts "End Time:\t#{end_time}\n"
            puts "---------- Benchmarking Finished ----------"
            result = end_time - start_time
            results.push result
            puts "Result:\t\t#{result} seconds"
            i += 1
        end
        results.sort!{|a,b| a <=> b}
        puts "#{results}"
    end
end

Benchmark = SimpleBenchmarker.new

SimpleBenchmarker.go 3 do
    time = rand(0.1..1.0)
    sleep time
end

Thanks for pointing that out, and responding so quickly. I've managed to get this working since.

1 Answer

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Charlotte Addison it looks like the issue is with the @run_count variable. The self.go method is a class method and not an instance method so it doesn't have access to @run_count. If you wanted to have access to that, you would have to make self.go an instance method by removing self. and calling it as a method.