学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Ruby | 23 | 構造体

↓↓クリックして頂けると励みになります。



22 | StringBuffer】 << 【ホーム

構造体を利用するプログラム
構造体を利用するプログラム



Rubyの構造体(Struct)は、Rubyプログラミング言語で複数のデータをグループ化するためのデータ構造です。
通常、複数の関連するデータを一つの単位として扱う必要がある場合に使用されます。

構造体は、クラスと似たような機能を提供しますが、定義が簡潔であり、オブジェクトを作成するための便利な方法を提供します。
通常、構造体は個々のフィールド(メンバー)を持ち、これらのフィールドには値が格納されます。

Visual Stdio Codeで以下のプログラムを作ってみましょう。


新規作成 【structtest1.rb】

class Structtest1

	def printElephant( e )

		puts '名前:' + e.name.to_s + ',' + '年齢:' + e.age.to_s + ',' 

+ '体重:' + e.weight.to_s

	end
end

Elephant = Struct.new(:name, :age, :weight)

e1 = Elephant.new()
e1.name = '花子'
e1.age = 8
e1.weight = 5000

e2 =  Elephant.new()
e2.name = '太郎'
e2.age = 7
e2.weight = 6000

s = Structtest1.new()

s.printElephant( e1 )
s.printElephant( e2 )



Rubyで構造体を定義するには以下のように行います。

構造体名(先頭大文字) = Struct.new(:要素1, :要素2, :要素3)



今回は「Elephant」という名の構造体で「name」「age」「weight」の3つの要素を持っています。

Elephant = Struct.new(:name, :age, :weight)



要素に値を代入するにはまずインスタンスを作ります。

#  インスタンスを作成
e1 = Elephant.new()

#各要素に値を代入
e1.name = '花子'
e1.age = 8
e1.weight = 5000



出力結果です。

~/Desktop/Programming/RB $ ruby structtest1.rb 

名前:花子,年齢:8,
名前:太郎,年齢:7,



Visual Stdio Codeで以下のプログラムを作ってみましょう。


新規作成 【structtest2.rb】

class Structtest2

	def printElephant( e )
		puts '名前:' + e.name.to_s + ',' + '年齢:' + e.age.to_s + ',' 

+ '体重:' + e.weight.to_s
	end

end

Elephant = Struct.new(:name, :age, :weight)
e = Array.new(3)

e[0] =Elephant.new()	
e[0].name = '花子'
e[0].age = 8
e[0].weight = 5000

e[1] = Elephant.new()
e[1].name = '太郎'
e[1].age = 7;
e[1].weight = 6000

e[2] = Elephant.new()
e[2].name = 'ゴン太'
e[2].age = 1
e[2].weight = 1000

s = Structtest2.new()

for i in 0..(e.length-1) do
	s.printElephant( e[i] )
end



この様に作成した構造体のインスタンスを配列に格納して利用することも出来ます。

Elephant = Struct.new(:name, :age, :weight)
e = Array.new(3)

e[0] =Elephant.new()	
e[0].name = '花子'
e[0].age = 8
e[0].weight = 5000



実行結果です。

~/Desktop/Programming/RB $ ruby structtest2.rb

名前:花子,年齢:8,
名前:太郎,年齢:7,
名前:ゴン太,年齢:1,



Visual Stdio Codeで以下のプログラムを作ってみましょう。


新規作成 【structtest3.rb】

class Structtest3

	def printElephantFamily( family )

		print '[父親]'
		printElephant( family.father )

		print '[母親]'
		printElephant( family.mother )

		for i in 0..(family.children.length-1) do
			print '[子供' + (i+1).to_s + ']'
			printElephant( family.children[i] )
		end
	end

	def printElephant( e )
		puts '名前:' + e.name.to_s + ',' + '年齢:' + e.age.to_s + ',' 

+ '体重:' + e.weight.to_s
	end
end

Elephantfamily=Struct.new(:father, :mother, :children)
Elephant = Struct.new(:name, :age, :weight)

ef =Elephantfamily.new()
ef.father = Elephant.new()

ef.father.name = '太郎'
ef.father.age = 7
ef.father.weight = 6000

ef.mother = Elephant.new()
ef.mother.name = '花子'
ef.mother.age = 8
ef.mother.weight = 5000

ef.children = Array.new(2)

ef.children[0] = Elephant.new()
ef.children[0].name = 'ゴン太'
ef.children[0].age = 1
ef.children[0].weight = 1000

ef.children[1] = Elephant.new()
ef.children[1].name = 'マリア'
ef.children[1].age = 0
ef.children[1].weight = 500

s=Structtest3.new()

s.printElephantFamily( ef )



構造体「Elephantfamily」と「Elephant」の2つを作り、2重のインスタンスを生成しています。

Elephantfamily=Struct.new(:father, :mother, :children)
Elephant = Struct.new(:name, :age, :weight)

ef =Elephantfamily.new()
ef.father = Elephant.new()



実行結果です。

~/Desktop/Programming/RB $ ruby structtest3.rb

[父親]名前:太郎,年齢:7,
[母親]名前:花子,年齢:8,
[子供1]名前:ゴン太,年齢:1,
[子供2]名前:マリア,年齢:0,



22 | StringBuffer】 << 【ホーム




↓↓クリックして頂けると励みになります。