Переменные-строки


# Инициализация строки
s0 = ""
s0 = String.new
s0 = "test"
s0 = String.new("test")
s0 = String("test")
s0 = %!test!
s0 = %#test#
s0 = %q/general single-quoted string/
s0 = %Q!general double-quoted string!

s0 = <<EOS
line1
line2
EOS

# Печать строки с отступом
print <<-STRING1, <<-STRING2
   Concat
   STRING1
      enate
      STRING2

# новая строка
s0="1"; puts s0.object_id;
# s1 - это та же строка!
s1 = s0.replace "2"; puts s1.object_id

# Размер
s0.empty?
len = s0.length
len = s0.size

# Склейка
s0 = "a""b"
s0 = "a"+"b"
s0 = "a"<<"b"
s0 = "a".concat "b"

# Подстроки
subcode = s0[3]
subchar = s0[3].chr
substr = s0[3,10]
substr = s0[0..3]  # = 0,1,2,3
substr = s0[0...3] # = 0,1,2
# вставка
s0.insert 3, "substr"
# замена
s0.gsub "find" "replace"
s0[3..10] = "substr"
searchstr = s0[/regexp/]
s0[/regexp/] = "replace"
s0.index("k")
# for multistring
s0.grep(/regexp/)
s0.delete "cc"
# delete with regexp
s0.delete "^cc"

# Сравнение
s0.equ? b
s0 == s1
s0 = "A" <=> "a"     # -1,0,1 - с учетом регистра
s0 = "A".casecmp "a" # -1,0,1 - без учета регистра

# Преобразования
s0.freeze   # = constant
s0.frozen?
s0.capitalize
s0.chomp
s0.chomp!
s0.chop
s0.chop!
s0.reverse
s0.reverse!
s0.split(/regexp/)
s0.downcase
s0.upcase
s0.swapcase
s0.ljust 50
s0.rjust 50
s0.center 50, '#'
s0.lstrip
s0.rstrip
s0.strip
s0.next
s0.succ
s0.each_byte { }

# integer, float, Symbol
s0.to_i  s0.to_f  s0.to_sym