Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby hash merge

h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge(h2)   #=> {"a"=>100, "b"=>254, "c"=>300}
h1.merge(h2){|key, oldval, newval| newval - oldval}
               #=> {"a"=>100, "b"=>54,  "c"=>300}
h1             #=> {"a"=>100, "b"=>200}
Comment

merge for hashes in rails

####################  Changes the original hash  ##################
h1.merge!(h2)
####################  returns a new hash  ##################
h1.merge(h2)
Comment

ruby hash merge vs merge!

h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge!(h2)   #=> {"a"=>100, "b"=>254, "c"=>300}
h1              #=> {"a"=>100, "b"=>254, "c"=>300}

h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge!(h2) { |key, v1, v2| v1 }
                #=> {"a"=>100, "b"=>200, "c"=>300}
h1              #=> {"a"=>100, "b"=>200, "c"=>300}
Comment

PREVIOUS NEXT
Code Example
Ruby :: conditional operator in ruby 
Ruby :: ruby how to loop through an array 
Ruby :: parse xml ruby 
Ruby :: ruby get the number of same element in array 
Ruby :: ruby boolean variable 
Ruby :: generate csv ruby 
Ruby :: ruby generate uuid 
Ruby :: ruby array append vs push 
Ruby :: ruby iterate hash with index 
Ruby :: rails logger info 
Ruby :: rails filter hashes by key value 
Ruby :: ruby get current pid 
Ruby :: rails transactions 
Ruby :: droptable rails 
Ruby :: ruby remove value from array 
Ruby :: ruby ternary operator 
Ruby :: ruby on rails binding.pry 
Ruby :: ruby check if path is a directory 
Ruby :: infinite loop in ruby 
Ruby :: ruby language 
Ruby :: ros2 publish message command line 
Ruby :: minimum of 3 elements 
Ruby :: ruby double star argument 
Ruby :: how to open ruby console 
Ruby :: rails ago 
Ruby :: print in rails 
Ruby :: ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline. 
R :: ggplot increase label font size 
R :: r na omit column 
R :: r print concatenate 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =