#!/usr/bin/ruby

# 문자열




## Substitution

s = "Hello, there"
str = s.sub(/[aeiou]/, '*') # 패턴과 일치하는 첫 번째 부분을 다른 문자열로 대체합니다.
str = s.gsub(/[aeiou]/, '*') # 패턴과 일치하는 모든 부분을 다른 문자열로 대체합니다.
puts str

## 공백 제거
str = s.strip
str = s.lstrip
str = s.rstrip

### 문자열을 배열처럼

str = "foo bar"
p str[0]
p str[0,3]


=begin
* length / size : 문자의 갯수
* bytesize
* empty?
* count : 일치하는 부분 문자열의 갯수를 반환

* index
* rindex
* include?
* match
* match?
* start_with?
* end_with?


* encoding
* unicode_normalized?
* valid_encoding?
* ascii_only?

* sum
* hash


* insert
* <<

* sub
* gsub
* succ
* next
* replace
* reverse
* setbyte
* tr
* tr_s
* dump
* undump


* capitalize
* downcase
* upcase
* swapcase


* encode
* unicode_normalize
* scrub
* force_encoding


* clear
* slice
* squeeze
* delete
* delete_prefix
* delete_suffix
* strip
* lstrip
* rstrip
* chomp
* chop
* chr
* byteslice



* center
* concat
* prepend
* ljust
* rjust

* to_s

* bytes
* chars
* getbyte

* lines
* partition
* rpartition
* split

* scan
* unpack


* hex
* oct
* to_i
* to_f




* each_byte
* each_char
* each_codepoint
* each_line



=end



## String IO

require 'stringio'

io = StringIO.new
io.close # 열었으면 닫아야 합니다.


StringIO.open {|io|
  # do something
  # 블록이 끝나면 클로즈됩니다.
}

str = <<STR
This is a text.
Hello, world!
The, bye.
STR

StringIO.open(str, 'r+'){|io|
  io.each_line{|line|
    puts "#{io.lineno}: #{line}"
  }
}

=begin
* binmode
* close
* close_read
* close_write
* closed?
* closed_read?
* closed_write?
* eof?

* each
* each_byte
* each_char
* each_codepoint
* each_line


* getbyte
* getc
* gets
* length
* lineno


=end

## String Scanner

require 'strscan'

scanner = StringScanner.new('Hello world!')
p scanner.scan(/\w+/)
p scanner.scan(/\s+/)
p scanner.eos?

=begin
* getch
* get_byte
* scan
* scan_until
* skip
* skip_until


* check
* check_until
* exist?
* match?
* peek


* beginning_of_line?
* eos?
* rest?
* rest_size
* pos
* reset
* terminate

* matched
* matched?
* matched_size
* pre_match
* post_match
* unscan

=end


END{
  # https://ruby-doc.org/3.3.0/String.html
  # https://ruby-doc.org/3.3.0/exts/stringio/StringIO.html
  # https://ruby-doc.org/3.3.0/exts/strscan/StringScanner.html
}
