2007年1月7日星期日

About Substituting Variables


Usually,we use such as #{var} for substituting variables into string.
eg.
  var="Allen"
  puts "My name is #{var} ."
  #=>"My name is Allen ."



And if you wanna use string interpolation more complex, you can do just like this:
  %{Here is #{class InstantClass
   def bar
    "some text"
   end
  end
  InstantClass.new.bar
  }.}
  # => "Here is some text."



Here, a way to avoid triggering string interpolation
1.Escape the hash characters
"\#{var}"    # => "\#{var}"
2.Put the string in single quotes
'#{var}'    # => "\#{var}"




Ways of multiline for string
1.Use %{}
2.Use =<< END .. END,here, 'END' is a tag of starting and ending for multiline string.

1 条评论:

琳琳的小狗 说...

其实python cookbook一样精彩,可惜出版社没引进,我会争取的,哈哈