简体   繁体   中英

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.

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")

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM