Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby freeze method

Tags:

ruby

call

freeze

def track_for stat
      # This is a hash with 2 elements of proc
      {
        symbol: -> { send(stat) },
        array:  -> { send(stat[0], stat[1]) }
      }.freeze[stat.class.name.underscore.to_sym].call
end

freeze[stat.class.name.underscore.to_sym].call , I have no idea about this code. What is the function of the code inside [], and why use call method? Anyone who can help me? Much appreciate it.

like image 450
Daniel Avatar asked Dec 27 '14 03:12

Daniel


People also ask

Can you freeze an array in Ruby?

Ruby Freeze method done following things on different objects, basically it's make object constant or immutable in ruby. But you can assign new string or change its reference. Once the reference change then it is not freeze (or constant) object. arr array is frozen means you can't change the array.

What is the use of freeze rails?

By using #freeze, I'm able to create a constant that's actually constant. This time, when I attempt to modify the string, I get a RuntimeError. Here' a real-world example of this in the ActionDispatch codebase. Rails hides sensitive data in logs by replacing it with the text "[FILTERED]".

What is the need to use freeze method?

Object.freeze() Method Freezing an object does not allow new properties to be added to an object and prevents from removing or altering the existing properties. Object. freeze() preserves the enumerability, configurability, writability and the prototype of the object.

Can you freeze a hash in Ruby?

You can always freeze your hashes in Ruby for safety if you want to. All that's missing is some sugar for declaring immutable hash literals.


1 Answers

Ruby Freeze method done following things on different objects, basically it's make object constant or immutable in ruby.

  1. String
str = "this is string"
str.freeze

str.replace("this is new string") #=> FrozenError (can't modify frozen String)
or
str[0] #=> 't'
str[0] = 'X' #=> FrozenError (can't modify frozen String)

But you can assign new string or change its reference. Once the reference change then it is not freeze (or constant) object.

str = "this is string"
str.freeze
str.frozen? #=> true
str = "this is new string"
str.frozen? #=> false

  1. Array :-
arr = ['a', 'b', 'c']
arr.freeze

arr << 'd' #=> FrozenError (can't modify frozen Array)
arr[0] = 'd' #=> FrozenError (can't modify frozen Array)
arr[1].replace('e') #=> ["a", "e", "c"]
arr.frozen? #=> true

arr array is frozen means you can’t change the array. But the strings inside the array aren’t frozen. If you do a replace operation on the string “one”, mischievously turning it into “e”, the new contents of the string are revealed when you reexamine the (still frozen!) array.

  1. Hash
hash = {a: '1', b: '2'}
hash.freeze

hash[:c] = '3' #=> FrozenError (can't modify frozen Hash)
hash.replace({c: '3'}) #=> FrozenError (can't modify frozen Hash)
hash.merge({c: '3'}) #=> return new hash {:a=>"1", :b=>"2", :c=>"3"}
hash.merge!({c: '4'}) #=> FrozenError (can't modify frozen Hash)
hash.frozen? #=> true

hash = {:a=>"1", :b=>"2", :c=>"3"}
hash.frozen? #=> false

So we can't modify the hash content but we can refer it to new hash (just like array)

like image 131
Kishor Vyavahare Avatar answered Oct 25 '22 12:10

Kishor Vyavahare