Newer
Older
Ruby / question.rb
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

class Question

  def initialize
  end

  def question(choices,mold = "moji")
    x = 0
    @select = -1
    if mold == "hyozi"
      while x < choices.length
        printf("%s ",choices[x])
        print("\e[m")
        x += 1
      end
    else
      while x < choices.length
        printf("%s(%d) ",choices[x],x)
        print("\e[m")
        x += 1
      end
    end
    print("\n")
    while @select < 0 || @select >= choices.length   #回答の仕分け
      print("選択:")
      @select = gets.chomp
      if @select == "" || check(@select) == false
        redo
      end
      @select = @select.to_i
    end
    if mold == "moji"
      @select = choices[@select]
      return @select
    elsif mold == "kazu"
      return @select
    else
      puts("Error")
    end
  end

  def check(h)
    number = ["1","2","3","4","5","6","6","7","8","9","0"]
    x = h.split("")
    n = 0
    for i in x
      if  false == number.include?(x[n])
        return false
      end
      n += 1
    end
    return true
  end

end

def question(choices,mold = "moji")
  x = 0
  select = -1
  if mold == "hyozi"
    while x < choices.length
      printf("%s ",choices[x])
      print("\e[m")
      x += 1
    end
  else
    while x < choices.length
      printf("%s(%d) ",choices[x],x)
      print("\e[m")
      x += 1
    end
  end
  print("\n")
  while select < 0 || select >= choices.length   #回答の仕分け
    print("選択:")
    select = gets.chomp
    if select == "" || check(select) == false
      redo
    end
    select = select.to_i
  end
  if mold == "moji"
    select = choices[select]
    return select
  elsif mold == "kazu"
    return select
  else
    puts("Error")
  end
end

def check(h)
  number = ["1","2","3","4","5","6","6","7","8","9","0"]
  x = h.split("")
  n = 0
  for i in x
      if  false == number.include?(x[n])
        return false
      end
      n += 1
  end
  return true
end

end