⭐️nilガードの書き方:

・nilガードの書き方「左辺があれば左辺を返し、なければ右辺を返す」

number ||= 10

例1):

① number ||= 10

このコードの意味は、

**「もしもnumberがあればnumberを返す、nilかfalseであれば10を代入した上でnumberを返す」**という意味になります。

①と②は同じ動きをします。

② number || (number = 10)

「左辺があれば左辺を返し、なければ右辺を返す」のが、nilガードです。

例2):

a ||= 10 #aはnilなので、aに10を代入してaを返します
=> 10

a ||= 20 #a = 10なので、そのまま左辺の10を返します。
=> 10
def build_body(controller)
    result = ''
    article_blocks.each do |article_block|
      result << if article_block.sentence? #「<<」は左辺と右辺が文字列として連結できるときって意味の「文字列演算子」
                  sentence = article_block.blockable
                  #binding.pry
                  sentence.body**||= ''** #sentence.bodyの中身がnilになっているからエラーを起こしているからメソッド呼び出し時に「nilガード」を使用する。
                elsif article_block.medium?
                  medium = ActiveDecorator::Decorator.instance.decorate(article_block.blockable)
                  controller.render_to_string("shared/_media_#{medium.media_type}", locals: { medium: medium }, layout: false)
                elsif article_block.embed?
                  embed = ActiveDecorator::Decorator.instance.decorate(article_block.blockable)
                  controller.render_to_string("shared/_embed_#{embed.embed_type}", locals: { embed: embed }, layout: false)
                end
    end

    result
  end