まずRubyでこのような構文を作った。
First I have this code written in ruby
Ruby
def initialize(init_array = [nil, nil, nil, nil, nil])
## 引数がnilなら、デフォルト値にします ## if ? true : false
init_array[0] ? main_text = init_array[0] : main_text = def_main_text
init_array[1] ? sub_text = init_array[1] : sub_text = def_sub_text
init_array[2] ? color_array = init_array[2] : color_array = def_color
init_array[3] ? width = init_array[3] : width = def_width
init_array[4] ? height = init_array[4] : height = def_height
## End of setting defaults numerals.
これはちょっと見辛いと思った。
以下のように変数を初期化したいのだけれど、
which of the below is better to read?
type1
## 引数がnilなら、デフォルト値にします ## if ? true : false
init_array[0] ? main_text = init_array[0] : main_text = def_main_text
init_array[1] ? sub_text = init_array[1] : sub_text = def_sub_text
init_array[2] ? color_array = init_array[2] : color_array = def_color
init_array[3] ? width = init_array[3] : width = def_width
init_array[4] ? height = init_array[4] : height = def_height
## End of setting defaults numerals.
type2
## 引数がnilなら、デフォルト値にします ## if ? true : false
init_array[0] ? main_text = init_array[0] : main_text = def_main_text
init_array[1] ? sub_text = init_array[1] : sub_text = def_sub_text
init_array[2] ? color_array = init_array[2] : color_array = def_color
init_array[3] ? width = init_array[3] : width = def_width
init_array[4] ? height = init_array[4] : height = def_height
## End of setting defaults numerals.
とここまで書いて思ったけど列挙しないほうがわかりやすいのかな…?
or maybe this is much better, using each
...?
type3
arr1 = [main_text, sub_text, color_array, width, height]
arr2 = [def_main_text, def_sub_text, def_color, def_width, def_height]
init_array.length.times do |num|
init_array[num] ? arr1[num] = init_array[num] : arr1[num] = arr2[num]
end