ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Ruby] .map // .select 2
    coding for food 2022. 8. 6. 03:58

     

    arr = ["apple", "bootCAMP", "caRrot", "DaNce"]
    
    new_arr = arr.map { |word| word[0].upcase + word[1..-1].downcase}
     
    print new_arr
    -------------------------------------
    #=>
    ["Apple", "Bootcamp", "Carrot", "Dance"]
    arr = ["apple", "bootCAMP", "caRrot", "DaNce"]
    
    new_arr = arr.map do |ele|
      first = ele[0].upcase
      rest = ele[1..-1].downcase
      first + rest
    end
    
    print new_arr
    ----------------------------------------------
    #=>
    ["Apple", "Bootcamp", "Carrot", "Dance"]
    arr = ["apple", "bootCAMP", "caRrot", "DaNce"]
    
    new_arr = arr.map.with_index do |ele, idx|
      first = ele[0].upcase
      rest = ele[1..-1].downcase
      new_word =first + rest
      new_word * idx  
    end
    
    print new_arr
    ------------------------------------------
    #=>
    ["", "Bootcamp", "CarrotCarrot", "DanceDanceDance"] 
    # idx 의 숫자만큼 곱해진다. apple의 idx는 0 이므로 empty.
Designed by Tistory.