ruby-freeze

    0

    0

    A hash object cannot be frozen.

    # Ruby freeze
    
    # Number
    num = 1.freeze
    num += 1              # No Error
    
    # String
    name = 'Tom'.freeze
    name << ' Hardy'      # FrozenError
    
    # Array
    texts = ['a', 'b', ['c', 'd'], {e: 'f'}].freeze
    texts[0] << 'b'       # No Error
    texts[1].replace('c') # No Error
    texts[2] << 'e'
    texts[3].merge!({g: 'h'})
    texts                 # => ["ab", "c", ["c", "d", "e"], {:e=>"f", :g=>"h"}]
    texts[0] = 'b'        # FrozenError
    
    hash = {a: 'b', c: 'd', e: ['f']}.freeze
    hash[:a] << 'b'
    hash[:c].replace('dd')
    hash[:e] << 'g'
    hash                  # => {:a=>"bb", :c=>"dd", :e=>["f", "g"]}
    hash[:a] = 1          # FrozenError
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.