{"id":1087,"date":"2017-01-31T19:28:41","date_gmt":"2017-01-31T17:28:41","guid":{"rendered":"http:\/\/www.lafabriquedecode.com\/blog\/?p=1087"},"modified":"2017-01-31T19:28:41","modified_gmt":"2017-01-31T17:28:41","slug":"modifier-la-cle-dun-hash-en-ruby","status":"publish","type":"post","link":"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/","title":{"rendered":"Modifier la cl\u00e9 d&rsquo;un Hash en Ruby"},"content":{"rendered":"<p>Si vous tentez de modifier la cl\u00e9 d&rsquo;un Hash en Ruby de fa\u00e7on pourtant \u00ab\u00a0instinctive\u00a0\u00bb, vous allez rencontrer des probl\u00e8mes&#8230;<\/p>\n<p><code><br \/>\nh = {\"cle\" => \"val\"}<br \/>\nh.keys[0].capitalize!<br \/>\n<\/code><\/p>\n<p><a href=\"http:\/\/www.lafabriquedecode.com\/blog\/wp-content\/uploads\/2017\/01\/16443237_1865407753716656_526630943_n.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.lafabriquedecode.com\/blog\/wp-content\/uploads\/2017\/01\/16443237_1865407753716656_526630943_n.jpg\" alt=\"16443237_1865407753716656_526630943_n\" width=\"711\" height=\"566\" class=\"alignnone size-full wp-image-1088\" srcset=\"http:\/\/www.lafabriquedecode.com\/blog\/wp-content\/uploads\/2017\/01\/16443237_1865407753716656_526630943_n.jpg 711w, http:\/\/www.lafabriquedecode.com\/blog\/wp-content\/uploads\/2017\/01\/16443237_1865407753716656_526630943_n-300x239.jpg 300w, http:\/\/www.lafabriquedecode.com\/blog\/wp-content\/uploads\/2017\/01\/16443237_1865407753716656_526630943_n-624x497.jpg 624w\" sizes=\"(max-width: 711px) 100vw, 711px\" \/><\/a><\/p>\n<p>Ruby n&rsquo;est pas tr\u00e8s content et c&rsquo;est un euph\u00e9misme !<\/p>\n<p><code>RuntimeError: can't modify frozen String<\/code><\/p>\n<p>Pour rem\u00e9dier \u00e0 \u00e7a, il suffit de passer par delete<br \/>\n<code>h = {\"cle\" => \"val\"}<br \/>\nh[h.keys[0].capitalize] = h.delete(h.keys[0])<\/code><\/p>\n<p>L&rsquo;appel \u00e0 delete va renvoyer la valeur associ\u00e9e \u00e0 votre cl\u00e9 pass\u00e9e en param\u00e8tre (h.keys[0]) et de ce fait, la mettre en face de votre nouvelle cl\u00e9, qui est ni plus ni moins que l&rsquo;ancienne avec une majuscule. L&rsquo;ancienne, elle, a bien \u00e9videmment disparu avec l&rsquo;appel \u00e0 delete.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Si vous tentez de modifier la cl\u00e9 d&rsquo;un Hash en Ruby de fa\u00e7on pourtant \u00ab\u00a0instinctive\u00a0\u00bb, vous allez rencontrer des probl\u00e8mes&#8230; h = {\u00ab\u00a0cle\u00a0\u00bb => \u00ab\u00a0val\u00a0\u00bb} h.keys[0].capitalize! Ruby n&rsquo;est pas tr\u00e8s content et c&rsquo;est un euph\u00e9misme ! RuntimeError: can&rsquo;t modify frozen String Pour rem\u00e9dier \u00e0 \u00e7a, il suffit de passer par delete h = {\u00ab\u00a0cle\u00a0\u00bb => [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[96],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.6.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Modifier la cl\u00e9 d&#039;un Hash en Ruby - La Fabrique de code - Tech blog<\/title>\n<meta name=\"description\" content=\"Comment changer la cl\u00e9 d&#039;un Hash alors que celle-ci est &quot;Frozen&quot; ? Eh bien cela vous prendra 2 minutes montre en main...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Modifier la cl\u00e9 d&#039;un Hash en Ruby - La Fabrique de code - Tech blog\" \/>\n<meta property=\"og:description\" content=\"Comment changer la cl\u00e9 d&#039;un Hash alors que celle-ci est &quot;Frozen&quot; ? Eh bien cela vous prendra 2 minutes montre en main...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/\" \/>\n<meta property=\"og:site_name\" content=\"La Fabrique de code - Tech blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-01-31T17:28:41+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.lafabriquedecode.com\/blog\/wp-content\/uploads\/2017\/01\/16443237_1865407753716656_526630943_n.jpg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@LaFabrique2Code\" \/>\n<meta name=\"twitter:site\" content=\"@LaFabrique2Code\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/\",\"url\":\"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/\",\"name\":\"Modifier la cl\u00e9 d'un Hash en Ruby - La Fabrique de code - Tech blog\",\"isPartOf\":{\"@id\":\"http:\/\/www.lafabriquedecode.com\/blog\/#website\"},\"datePublished\":\"2017-01-31T17:28:41+00:00\",\"dateModified\":\"2017-01-31T17:28:41+00:00\",\"author\":{\"@id\":\"http:\/\/www.lafabriquedecode.com\/blog\/#\/schema\/person\/83863c048b82fd9ccf6407bddd241162\"},\"description\":\"Comment changer la cl\u00e9 d'un Hash alors que celle-ci est \\\"Frozen\\\" ? Eh bien cela vous prendra 2 minutes montre en main...\",\"breadcrumb\":{\"@id\":\"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"http:\/\/www.lafabriquedecode.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Modifier la cl\u00e9 d&rsquo;un Hash en Ruby\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.lafabriquedecode.com\/blog\/#website\",\"url\":\"http:\/\/www.lafabriquedecode.com\/blog\/\",\"name\":\"La Fabrique de code - Tech blog\",\"description\":\"PHP objet, MySQL, Design Patterns, OOP...et plus !\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.lafabriquedecode.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.lafabriquedecode.com\/blog\/#\/schema\/person\/83863c048b82fd9ccf6407bddd241162\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"http:\/\/www.lafabriquedecode.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/0.gravatar.com\/avatar\/fc2e1de7c8a1871b50ff9c6a6f8682a2?s=96&d=retro&r=g\",\"contentUrl\":\"http:\/\/0.gravatar.com\/avatar\/fc2e1de7c8a1871b50ff9c6a6f8682a2?s=96&d=retro&r=g\",\"caption\":\"admin\"},\"url\":\"http:\/\/www.lafabriquedecode.com\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Modifier la cl\u00e9 d'un Hash en Ruby - La Fabrique de code - Tech blog","description":"Comment changer la cl\u00e9 d'un Hash alors que celle-ci est \"Frozen\" ? Eh bien cela vous prendra 2 minutes montre en main...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/","og_locale":"fr_FR","og_type":"article","og_title":"Modifier la cl\u00e9 d'un Hash en Ruby - La Fabrique de code - Tech blog","og_description":"Comment changer la cl\u00e9 d'un Hash alors que celle-ci est \"Frozen\" ? Eh bien cela vous prendra 2 minutes montre en main...","og_url":"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/","og_site_name":"La Fabrique de code - Tech blog","article_published_time":"2017-01-31T17:28:41+00:00","og_image":[{"url":"http:\/\/www.lafabriquedecode.com\/blog\/wp-content\/uploads\/2017\/01\/16443237_1865407753716656_526630943_n.jpg"}],"author":"admin","twitter_card":"summary","twitter_creator":"@LaFabrique2Code","twitter_site":"@LaFabrique2Code","twitter_misc":{"\u00c9crit par":"admin","Dur\u00e9e de lecture estim\u00e9e":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/","url":"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/","name":"Modifier la cl\u00e9 d'un Hash en Ruby - La Fabrique de code - Tech blog","isPartOf":{"@id":"http:\/\/www.lafabriquedecode.com\/blog\/#website"},"datePublished":"2017-01-31T17:28:41+00:00","dateModified":"2017-01-31T17:28:41+00:00","author":{"@id":"http:\/\/www.lafabriquedecode.com\/blog\/#\/schema\/person\/83863c048b82fd9ccf6407bddd241162"},"description":"Comment changer la cl\u00e9 d'un Hash alors que celle-ci est \"Frozen\" ? Eh bien cela vous prendra 2 minutes montre en main...","breadcrumb":{"@id":"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.lafabriquedecode.com\/blog\/2017\/01\/modifier-la-cle-dun-hash-en-ruby\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"http:\/\/www.lafabriquedecode.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Modifier la cl\u00e9 d&rsquo;un Hash en Ruby"}]},{"@type":"WebSite","@id":"http:\/\/www.lafabriquedecode.com\/blog\/#website","url":"http:\/\/www.lafabriquedecode.com\/blog\/","name":"La Fabrique de code - Tech blog","description":"PHP objet, MySQL, Design Patterns, OOP...et plus !","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.lafabriquedecode.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"fr-FR"},{"@type":"Person","@id":"http:\/\/www.lafabriquedecode.com\/blog\/#\/schema\/person\/83863c048b82fd9ccf6407bddd241162","name":"admin","image":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"http:\/\/www.lafabriquedecode.com\/blog\/#\/schema\/person\/image\/","url":"http:\/\/0.gravatar.com\/avatar\/fc2e1de7c8a1871b50ff9c6a6f8682a2?s=96&d=retro&r=g","contentUrl":"http:\/\/0.gravatar.com\/avatar\/fc2e1de7c8a1871b50ff9c6a6f8682a2?s=96&d=retro&r=g","caption":"admin"},"url":"http:\/\/www.lafabriquedecode.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"http:\/\/www.lafabriquedecode.com\/blog\/wp-json\/wp\/v2\/posts\/1087"}],"collection":[{"href":"http:\/\/www.lafabriquedecode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.lafabriquedecode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.lafabriquedecode.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.lafabriquedecode.com\/blog\/wp-json\/wp\/v2\/comments?post=1087"}],"version-history":[{"count":3,"href":"http:\/\/www.lafabriquedecode.com\/blog\/wp-json\/wp\/v2\/posts\/1087\/revisions"}],"predecessor-version":[{"id":1091,"href":"http:\/\/www.lafabriquedecode.com\/blog\/wp-json\/wp\/v2\/posts\/1087\/revisions\/1091"}],"wp:attachment":[{"href":"http:\/\/www.lafabriquedecode.com\/blog\/wp-json\/wp\/v2\/media?parent=1087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.lafabriquedecode.com\/blog\/wp-json\/wp\/v2\/categories?post=1087"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.lafabriquedecode.com\/blog\/wp-json\/wp\/v2\/tags?post=1087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}