OSDN Git Service

refactored complex_number_arithmetic generator
authorTomohiro Nishimura <tomohiro68@gmail.com>
Sun, 21 Mar 2010 17:41:50 +0000 (02:41 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Sun, 21 Mar 2010 17:41:50 +0000 (02:41 +0900)
lib/mint/generator/complex_number_arithmetic.rb

index afdc9cb..6991f03 100644 (file)
@@ -35,29 +35,25 @@ module Mint::Generator
     end
 
     def operand
-      result =
-        if options[:fractional_mode]
-          create_fractional_complex_number
-        else
-          create_complex_number
-        end
-      return "#{result}^2" if options[:term_number] == 1
-      return conjugate_complex(last_operand) if last_operator == '/'
-      result
+      return create_fractional_complex_number if options[:fractional_mode]
+      return "#{create_complex_number}^2"     if options[:term_number] == 1
+      return conjugate_complex(last_operand)  if last_operator == '/'
+      create_complex_number
     end
 
     def create_complex_number
-      real_part = create_integer(options[:min], options[:max], false)
-      imaginary_part =  create_integer(options[:min], options[:max], false)
-      return (2 + rand(8)).to_s if [real_part, imaginary_part].include? 0
-      operator =  %w[ + - ].sample
-      "(#{real_part} #{operator} #{imaginary_part.to_s.sub('1', '')}%i)"
+      real_part      = create_integer(options[:min], options[:max], false)
+      imaginary_part = create_integer(options[:min], options[:max], false)
+      return create_integer(2, 9, false).to_s if [real_part, imaginary_part].include? 0
+      "(#{real_part} #{plus_or_minus} #{imaginary_part.to_s.sub('1', '')}%i)"
+    end
+
+    def plus_or_minus
+      %w[ + - ].sample
     end
 
     def create_fractional_complex_number
-      numerator_part = 1 + rand(9)
-      denominator_part = create_complex_number
-      "#{numerator_part} / #{denominator_part}"
+      "#{create_integer(1, 9, false)} / #{create_complex_number}"
     end
 
     def conjugate_complex(expression)