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

Even numbers

Hello all,

I spent a bit of tonight getting to grips with ruby and programming in general. I have written a program to get the even numbers out from an array of numbers. However once I run this I get output such as -

Your number is You're number is 2 Your number is You're number is 4

Below is the code

class EvenNumbers
    def initialize 
        @numbers = [1,2,3,4,5,6,7,8,9,10]
    end
    def sort_number
        @numbers.each do |number|
            @number = sort(number)
            yield @number
        end
    end
    def sort(number)
        evencheck = number % 2
        if evencheck == 0
            return number 
        end
    end
end
evennumbers = EvenNumbers.new
evennumbers.sort_number do |number|
    puts "Odd number is #{number}"
end

4 Answers

James,

Without giving the answer, I can tell you that if you go through each line of the code you posted, and say out loud "this line does x", you'll see why the repeat's happening.

Thank you James,

Your reply was better than an actual answer :)

Cool, glad it helped. I know how frustrating it can be. I've found it really useful to "explain" the code out loud, especially shorter code blocks--and it often helps me understand the material better, too.

Yeah I found it helped, I was just using the yield in the wrong context. Got back on it this afternoon and was able to make the following -

class EvenNumbers
    def initialize 
        @numbers = []
        @even = []
        @odd = []
        while get_array
            puts "Line added"
        end
        sort_number
    end
    def get_array
        puts "Enter a NUMBER [to stop pass in 000]"
        userinput = gets.chomp
        if userinput != "000" and userinput.length > 0
            input = userinput.to_i
            @numbers.push input
        else
            return nil
        end
    end
    def sort_number
        array = @numbers.to_s 
        puts "Orginal array consits of #{array}"
        @numbers.each do |number|
            @number = sort(number)
        end
    end
    def sort(number)
        evencheck = number % 2
        if evencheck == 0
            @even.push number
        else
            @odd.push number
        end
    end
    def each_even(&block)
        @even.each {|even| yield even}
    end
    def each_odd(&block)
        @odd.each{|odd| yield odd}
    end

end
evennumbers = EvenNumbers.new
evennumbers.each_even do |even|
    puts "[Even numbers] - #{even}"
end
evennumbers.each_odd do |odd|
    puts "[Odd numbers] - #{odd}"
end

Works great :)