Using %i and %I symbol array literal

前端 未结 2 1013
太阳男子
太阳男子 2021-02-18 13:58

Reading through a list of Rails questions, I\'m having trouble finding what the %i does in relation to a symbol array. Does this mean anything to anyone?

2条回答
  •  故里飘歌
    2021-02-18 14:15

    Lowercase %i stands for

    Non-interpolated Array of symbols, separated by whitespace (after Ruby 2.0)

    In addition, uppercase %I means

    Interpolated Array of symbols, separated by whitespace (after Ruby 2.0)

    Example of difference regarding interpolation:

    2.4.2 :001 > a = 1
    2.4.2 :002 > %i{one two #{a}+three} # Interpolation is ignored
     => [:one, :two, :"\#{a}+three"]
    2.4.2 :003 > %I{one two #{a}+three} # Interpolation works
     => [:one, :two, :"1+three"]
    

    Have a look at here for further information.

提交回复
热议问题