1

ActiveRecordを含める方法を意味するのではありませんが、説明させてください。

GameとオブジェクトdifficultyLevelIDが欲しいです。DifficultyLevel

RailsとActiveRecord(私が精通しているもの)では、これらはテーブルであり、メソッドhas_manybelongs_toメソッドがあり、それを使用しdifficultyLevelIDて物事を取得できるため、難易度は次のようになります。Game.difficulty_level.name

データベースなしでRubyプログラムを実行していて、その関係を使用したい場合、つまりGameID難易度レベルとレベルname自体をdifficultiesクラスに含めたい場合、それを行うにはどうすればよいですか(作成、維持、クエリ関係)Rubyとだけで、ゲームの難易度の名前を取得できますか?

4

1 に答える 1

0

20時間経っても返事がなかったので、自分で投稿しました。

class Soduko
  attr_accessor :name, :rows, :columns, :difficulty_level
  def initialize // will probably move to parameters as defaults.
    @rows= 9
    @columns= 9
    @name= 'My Soduko'
    @difficulty_level= 'Medium'
  end

  def initial_number_count
    DifficultyLevel.start_with_numbers('Medium')
  end

end

class DifficultyLevel

  def self.start_with_numbers(difficulty_level)
    case difficulty_level
      when 'Easy'
      then 30
      when 'Medium'
      then 20
      when 'Hard'
      then 10
      else 20
    end

  end

end

そしてもちろんテスト:

require './soduko'

describe Soduko, '.new' do

  before { @soduko_board  = Soduko.new }

  it "Should allow for a new Board with 9 rows (default) to be created" do
    @soduko_board.rows.should == 9
  end 

  it "Should allow for a new Board with 9 columns (default) to be created" do
    @soduko_board.columns.should == 9
  end 

  it "should have a default difficulty level of 'Medium'" do
    @soduko_board.difficulty_level.should == 'Medium'
  end 

  it "should have 10 initial numbers" do
    @soduko_board.initial_number_count.should == 20
  end 

end

describe DifficultyLevel, '.new' do

  it "should exist" do
    @difficulty_level = DifficultyLevel.new
  end

  # More to be added...

end
于 2012-05-06T17:25:05.047 に答える