Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | ブロック | 基礎

パンくず

Ruby
ブロック
基礎

概要

ブロックについて

内容

Rubyのブロックの基本的なパターンをサンプルとして例示します

構文

yieldによってブロック内のコードを呼び出すことが出来ます。

yield param...

サンプル

require "pp"

class Person
  attr_accessor:first_name,:family_name
  def initialize(first_name,family_name)
    @first_name = first_name
    @family_name = family_name
  end
end

def fullname(person)
  yield "#{person.first_name} #{person.family_name}"
end 

tanaka = Person.new("tanaka","yoshio")
fullname(tanaka){|fullname| puts fullname}

出力

tanaka yoshio