분류 전체보기
-
[Ruby] exercise _ Retrieve Valuescoding for food 2022. 8. 5. 08:19
Write a method retrieve_values that takes in two hashes and a key. The method should return an array containing the values from the two hashes that correspond with the given key. def retrieve_values(hash1, hash2, key) end dog1 = {"name"=>"Fido", "color"=>"brown"} dog2 = {"name"=>"Spot", "color"=> "white"} print retrieve_values(dog1, dog2, "name") #=> ["Fido", "Spot"] puts print retrieve_values(d..
-
[Ruby] exercise _ Word Lengthscoding for food 2022. 8. 5. 04:50
Write a method word_lengths that takes in a sentence string and returns a hash where every key is a word of the sentence, and its' corresponding value is the length of that word. def word_lengths(sentence) end puts word_lengths("this is fun") #=> {"this"=>4, "is"=>2, "fun"=>3} puts word_lengths("When in doubt, leave it out") #=> {"When"=>4, "in"=>2, "doubt,"=>6, "leave"=>5, "it"=>2, "out"=>3} de..
-
[Ruby] exercise _ Get Double Agecoding for food 2022. 8. 5. 02:35
Write a method get_double_age that takes in a hash and returns twice the "age" value of the hash. def get_double_age(hash) end puts get_double_age({"name"=>"App Academy", "age"=>5}) # => 10 puts get_double_age({"name"=>"Ruby", "age"=>23}) # => 46 def get_double_age(hash) return hash["age"] * 2 end puts get_double_age({"name"=>"App Academy", "age"=>5}) # => 10 puts get_double_age({"name"=>"Ruby",..
-
-
[Ruby] hashes // # default value : nil # Hash.new(default) # container hash strategycoding for food 2022. 8. 4. 08:03
# default value nil my_hash = { "a" => 28 } puts my_hash["b"] == nil # nil is a special value we have in Ruby. # basic represents nothingness or emptiness. puts my_hash["a"] == nil ------------------------------------------------ # => true false # Hash.new(default) my_hash = Hash.new(0) # default 값을 0으로 줬음 puts my_hash # my hash 라는 저장 장소에 아무것도 없어서 빈것으로 나옴 puts my_hash["a"] # key 값이 없으므로 default ..
-
[Ruby] #hash enumerables #.each #.each_key #.each_valuecoding for food 2022. 8. 4. 03:54
pizza = { "style" => "New York", "slices" => 8, "diameter" => "15 inches", "toppings" => ["mushrooms", "greenpeppers"], "is_tasty" => true } pizza.each do |k, v| puts k puts v puts "------" end pizza.each_key do |things| puts things end pizza.each_value do |val| puts val end -------------------------------------- #=> style New York ------ slices 8 ------ diameter 15 inches ------ toppings mushro..
-
[Ruby] hashes data typecoding for food 2022. 8. 4. 03:40
# good array data animals = ["dog", "cat", "fish", "bird", "rabbit"] # not so good array data person = ["Kim", 100, "New York", "burger", true ] # better as hash better_person = { "name" => "Kim", "age" => 100, "location" => "New York", "favoritFood" => "burger", "isHungry" => true } puts better_person["location"] --------------------------------- #=> New York
-
[Ruby] Hash Methods _ #.length#.has_key?(k)#.has_value?(v)#.keys#.valuescoding for food 2022. 8. 4. 02:07
dog = { "name" => "Fido", "color" => "black", "age" => 3, "isHungry" => "true", "enemies" => ["squirrel"], } dog["location"] = "NY" dog["age"] += 10 dog["enemies"] {"name"=>"Fido", "color"=>"black", "age"=>13, "isHungry"=>"true", "enemies"=>["squirrel", "mailman"], "location"=>"NY"} dog = { "name" => "Fido", "color" => "black", "age" => 3, "isHungry" => "true", "enemies" => ["squirrel"], } puts ..