Using %i and %I symbol array literal

前端 未结 2 1059
太阳男子
太阳男子 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.

    0 讨论(0)
  • 2021-02-18 14:18

    I'm having trouble finding what the %i does in relation to a symbol array.

    It is an array literal for an array of symbols. It does the same thing in relation to symbol arrays as ' does to strings.

    0 讨论(0)
提交回复
热议问题