#!/usr/bin/ruby

require 'optparse'
require 'ostruct'

# == NAME
#
#   Euler002 - This is a script which works out the solution of a Euler
#   problem.
#
# == SYNOPSYS
#
#   Euler002.pm [OPTION]... [PARAMETER]...
#
# == DESCRIPTION
#
#   Euler002 - This script is to solve the following Euler Problem:
#
#   Each new term in the Fibonacci sequence is generated by adding the
#   previous two terms. By starting with 1 and 2, the first 10 terms
#   will be:
#
#   1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
#
#   By considering the terms in the Fibonacci sequence whose values
#   do not exceed four million, find the sum of the even-valued terms.
# == OPTIONS
#
#   -r, --restrict
#
#       This is the primary restriction for this Euler Problem.
#
# == LICENSE
#
#   Copyright 2018 James Carson
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included
#   in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
#   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
#   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
class Euler002
    def initialize( restrict_1 )
        @restrict = restrict_1.to_i
    end

    def run
        x = 0
        answer = 0
        value_this = 1
        value_next = 2
        value_last = 0
        answer = 0

        while value_this < @restrict do
            if value_this % 2 == 0
                answer += value_this
            end
            value_next = next_fib_number(value_this, value_last)
            value_last = value_this
            value_this = value_next
        end

        return answer
    end

    def next_fib_number(value_this, value_last)
        return (value_this + value_last)
    end
end


if __FILE__ == $0

    options = OpenStruct.new

    OptionParser.new do |opt|
        opt.on('-r', '--restrict RESTRICTION', 'The primary restriction for this Euler Problem')  { |o| options.restrict = o }
    end.parse!

    euler = Euler002.new( options.restrict )
    euler.run()

end
