(u_u)

Crystal

Categories: [blog]


crystal入門:正規表現でマッチした文字列をすべて返す:[String] #scan(pattern : Regex)


Categories: [blog]

def scan(pattern : Regex)

Searches the string for instances of pattern, returning an Array of Regex::MatchData for each match.

match_data = "hello".scan(/he/)
match_data.each_index do |i|
    puts match_data[i][0]
end

Continue Reading...


crystal入門:文字置換:[String] #gsub()


Categories: [blog]

  • 1文字
    def gsub(hash : Hash(Char, UNDERSCORE))

    Returns a String where all chars in the given hash are replaced by the corresponding hash values.

    "hello".gsub({'e' => 'a', 'l' => 'd'}) # => "haddo"
  • 正規表現

     def gsub(pattern : Regex, replacement, backreferences = true)

    Returns a String wh...


crystal入門:割り切れる?:[Int] #divisible_by?(num)


Categories: [blog]

Int

10.divisible_by?(5) #=> true
11.divisible_by?(5) #=> false

crystal/int.cr at fbfe8b62f44eca4596090b7580801bd184f73c7a · crystal-lang/crystal

def divisible_by?(num)
    self % num == 0
end

crystal入門:あまりのない割り算:[Int] #//


Categories: [blog]

1 // 2 

crystal/int.cr at fbfe8b62f44eca4596090b7580801bd184f73c7a · crystal-lang/crystal より、0.29.0からInt#/を利用すると返り値はFloatになるように変更されたようです。

@[Deprecated("Int#/ will return a Float in 0.29.0. Use Int#// for integer division.")]
  def /(other : Int)
    self // other
end

Continue Reading...