Tyler Rash
Tyler Rash

Reputation: 1173

How to conditionally assign Ruby variables to object instances

I have a Team class that contains a find_team class method. The method returns a Team object if one is found, or nil if no object is found:

class Team
  @@all_teams = []

  def self.find_team(name)
    index = @@all_teams.find_index { |t| t.name == name }

    if index.nil?
      nil
    else
      @@all_teams[index]
    end
  end
end

In implementing the class, I need to assign a variable to the correct Team object if it exists or create a new Team object if it doesn't. I'm struggling with finding the clearest, quickest, ruby-ist way to do it. Some options I've considered:

t1 = Team.find_team("Some Team") ? Team.find_team("Some Team") : Team.new("Some Team")

t2 = Team.find_team("Some Other Team")
t2 ||= Team.new("Some Other Team")

t3 = if Team.find_team("Another Team")
       Team.find_team("Another Team")
     else
       Team.new("Another Team")

I favor the second example, as it's less verbose and repetitive, but I'm wondering if there's some Ruby trick I'm missing in this scenario.

Upvotes: 1

Views: 294

Answers (2)

Simone Carletti
Simone Carletti

Reputation: 176442

t1 = Team.find_team("Some Team") || Team.new("Some Team")

is probably the most Ruby-ish version. You can also consider to enhance your custom method.

As a side note, you can refactor your find_team method to use Enumerable#find

class Team
  @@all_teams = []

  def self.find_team(name)
    @@all_teams.find { |t| t.name == name }
  end
end

And you can also add a new method that returns the instance if none

class Team
  @@all_teams = []

  def self.find_team(name)
    @@all_teams.find { |t| t.name == name }
  end

  def self.find_or_build_team(name)
    find_team(name) || Team.new(name)
  end
end

so that you will just call

Team.find_or_build("Some Team")

Upvotes: 3

Marek Lipka
Marek Lipka

Reputation: 51171

How about:

t1 = Team.find_team('Some Team') || Team.new('Some Team')

This assigns a new Team instance into t1 only if find_team method returns nil.

Upvotes: 1

Related Questions