This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # h = {a: 1, b:2, c: 3.5, d: 'hello', e: 15.5, f: 'Good', g:15, h: "Morning!", i:1.5} | |
| # Create Integer hash, Floating hash, String hash. | |
| # Ex: | |
| # string_hash = {a:'hello', b: 'Good', c: 'Morning!'} | |
| # int_hash= {a: 1,b:2,c:15} | |
| hs = {a: 1, b:2, c: 3.5, d: 'hello', e: 15.5, f: 'Good', g:15, h: "Morning!", i:1.5} | |
| int_hash = {} | |
| string_hash = {} | |
| float_hash = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # h = {a: 1, b:2, c: 3, 'd' => 4, 'e' => 5, g: 'my name is namizuko', h: 15} | |
| # calculate the sum of all integer values | |
| hs = {a: 1,b: 2,c: 3,'d' => 4,'e' => 5,g: 'my name is Ravindra',h: 15} | |
| sum = 0 | |
| hs.each do |key,value| | |
| if value.class == Integer | |
| sum += value | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # h1 = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15} | |
| # Make this hash into descending order. | |
| hs = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15} | |
| puts "Hash is Descending is :" | |
| puts "#{hs.sort_by {|k,v| v}.reverse}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # h1 = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15} | |
| # Make this hash into ascending order. | |
| hs = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15} | |
| puts "Hash is Ascending is :" | |
| puts "#{hs.sort_by {|k,v| v}}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # h1 = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15} | |
| # Find the keys of Max and Min value. | |
| hs = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15} | |
| max = 0 | |
| min = hs[:a] | |
| hs.each do |key,value| | |
| if value > max | |
| max = value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # h1 = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15} | |
| # Find the Max and Min value. | |
| hs = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15} | |
| max = 0 | |
| min = hs[:a] | |
| hs.each do |key,value| | |
| if value > max | |
| max = value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # h1 = {a: 1, b:2, c: 3} h2 = {'d'=> 4, 'e' => 5, h: 15} | |
| # Marge both the hash value. | |
| hs1 = {a: 1, b:2, c: 3} | |
| hs2 = {'d'=> 4, 'e' => 5, h: 15} | |
| puts hs1.merge(hs2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # h = {a: 1, b:2, c: 3, 'd' => 4, 'e' => 5, h: 15} | |
| # calculate the sum of all Even values | |
| hs = {a: 1, b:2, c: 3, 'd' => 4, 'e' => 5, h: 15} | |
| sum = 0 | |
| hs.each do |key,value| | |
| if value % 2 ==0 | |
| sum += value | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # h = {a: 1, b:2, c: 3, 'd' => 4, 'e' => 5, h: 15} | |
| # calculate the sum off all Odd values | |
| hs = {a: 1, b:2, c: 3, 'd' => 4, 'e' => 5, h: 15} | |
| sum = 0 | |
| hs.each do |key,value| | |
| if value % 2 !=0 | |
| sum += value | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Swap string values with out using third variable | |
| str1 = "This is String 1 !" | |
| str2 = "This is String 2 !" | |
| puts "Before swapping :" | |
| puts "String 1 : #{str1}" | |
| puts "String 2 : #{str2}" |
NewerOlder