Why doesn't Ruby have a real StringBuffer or StringIO?

后端 未结 5 543
情话喂你
情话喂你 2020-12-09 07:25

I recently read a nice post on using StringIO in Ruby. What the author doesn\'t mention, though, is that StringIO is just an \"I.\" There\'s no \

5条回答
  •  时光取名叫无心
    2020-12-09 07:47

    Well, a StringBuffer is not quite as necessary in Ruby, mainly because Strings in Ruby are mutable... thus you can build up a string by modifying the existing string instead of constructing new strings with each concat.

    As a note, you can also use special string syntax where you can build a string which references other variables within the string, which makes for very readable string construction. Consider:

    first = "Mike"
    last = "Stone"
    name = "#{first} #{last}"
    

    These strings can also contain expressions, not just variables... such as:

    str = "The count will be: #{count + 1}"
    count = count + 1
    

提交回复
热议问题