LoginSignup
2
2

More than 5 years have passed since last update.

列挙する場合のRubyでのインデント?揃え(Indenting? in ruby)

Last updated at Posted at 2014-11-20

まず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

2
2
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2