{"id":216,"date":"2016-08-12T09:08:58","date_gmt":"2016-08-12T13:08:58","guid":{"rendered":"https:\/\/mberlove.com\/blog\/?p=216"},"modified":"2016-08-11T23:09:59","modified_gmt":"2016-08-12T03:09:59","slug":"more-ruby-miscellanea","status":"publish","type":"post","link":"https:\/\/mberlove.com\/blog\/more-ruby-miscellanea\/","title":{"rendered":"More Ruby Miscellanea"},"content":{"rendered":"<p><a href=\"https:\/\/mberlove.com\/blog\/wp-content\/uploads\/2016\/08\/cubes-677092_1280.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-217\" src=\"https:\/\/mberlove.com\/blog\/wp-content\/uploads\/2016\/08\/cubes-677092_1280-1024x576.png\" alt=\"cubes-677092_1280\" width=\"590\" height=\"332\" srcset=\"https:\/\/mberlove.com\/blog\/wp-content\/uploads\/2016\/08\/cubes-677092_1280-1024x576.png 1024w, https:\/\/mberlove.com\/blog\/wp-content\/uploads\/2016\/08\/cubes-677092_1280-300x169.png 300w, https:\/\/mberlove.com\/blog\/wp-content\/uploads\/2016\/08\/cubes-677092_1280-768x432.png 768w, https:\/\/mberlove.com\/blog\/wp-content\/uploads\/2016\/08\/cubes-677092_1280.png 1280w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p>I like to chronicle the little discoveries and revelations in my day&#8217;s work. It helps keep me clearheaded, gives me a reference for later, and lets me share my knowledge with the world. Granted, in large part, these &#8220;discoveries&#8221; have long since been &#8220;discovered&#8221; &#8212; but even in well-documented libraries and languages, it&#8217;s surprising how easily knowledge gets lost in the shuffle.<\/p>\n<p>&nbsp;<\/p>\n<p>With that in mind, here are a few little bits of Ruby trivia I&#8217;ve run across in recent days. May they help you in your own work, too.<\/p>\n<h1><\/h1>\n<h1>Varieties of string quoting<\/h1>\n<p>There are many ways to use strings in Ruby, with single quotes, double quotes, heredocs, and string interpolation.<\/p>\n<p>Here&#8217;s a useful discussion on the topic from StackOverflow: <a href=\"http:\/\/stackoverflow.com\/questions\/279270\/which-style-of-ruby-string-quoting-do-you-favour\">http:\/\/stackoverflow.com\/questions\/279270\/which-style-of-ruby-string-quoting-do-you-favour<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>String interpolation in particular was foreign to me, and it turns out there are <a href=\"https:\/\/simpleror.wordpress.com\/2009\/03\/15\/q-q-w-w-x-r-s\/\">several different ways it can be used<\/a>! In short it&#8217;s a useful way to get around escaping or changing characters such as quotation marks in your strings (and it can do other stuff too).<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Here_document#Ruby\">Heredocs in Ruby <\/a>are a useful technique as well; it&#8217;s not unique to Ruby, of course, but a great option to have around for setting of blocks of text. It turns out, by the way, that heredocs don&#8217;t play well with hashes in Ruby, syntactically. It&#8217;s tough to make them work right, and you might be better off using a different type of quote in your hashes, even for longer strings. It took me a frustratingly long time to discover that particular item!<\/p>\n<p>Anyway, here&#8217;s a <a href=\"http:\/\/log.gmarik.info\/2007\/12\/rubys-here-document-heredoc-mini.html\">nice breakdown of various heredoc features in Ruby<\/a>.<\/p>\n<h1><\/h1>\n<h1>Predicting remote files<\/h1>\n<p>Ruby is great at grabbing data from a remote file &#8212; you can treat data from an online file almost identically to how you read any other file input. That&#8217;s all well and good so long as you know the file type before you work with it. But what if you don&#8217;t?<\/p>\n<p>&nbsp;<\/p>\n<p>That was the situation I ran across recently, and it had me stumped. So I thought about how to find out what the file was before you even grabbed the data &#8212; and it turns out there&#8217;s a pretty nifty way to do that that&#8217;s even in Ruby&#8217;s standard library! So it&#8217;s very easy to use. <a href=\"http:\/\/stackoverflow.com\/questions\/9543629\/verifying-a-remote-image-is-actually-an-image-file-in-ruby\">Here is the example<\/a> that I found, and it&#8217;s very easy to adjust it to your needs.<\/p>\n<h1><\/h1>\n<h1>Grep in hashes<\/h1>\n<p>You probably know grep; it&#8217;s <a href=\"https:\/\/www.google.com\/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=3&amp;cad=rja&amp;uact=8&amp;ved=0ahUKEwiE58658brOAhVJWRoKHcalCeQQFgg3MAI&amp;url=http%3A%2F%2Fwww.gnu.org%2Fs%2Fgrep%2Fmanual%2Fgrep.html&amp;usg=AFQjCNFaGoNC6HLx6Ll0D7aqBGvvsLxtww&amp;sig2=r_FDQgG5T9pMpqjQX6ER5g\">that multi-use tool for finding and searching text<\/a>. Well, turns out (as I discovered while <a href=\"https:\/\/www.google.com\/search?q=ruby+grep+hash+key&amp;ie=utf-8&amp;oe=utf-8\">searching on a whim<\/a>) that you can in fact <a href=\"https:\/\/books.google.com\/books?id=xBmkBwAAQBAJ&amp;pg=PT350&amp;lpg=PT350&amp;dq=ruby+grep+hash+key&amp;source=bl&amp;ots=yyZBwMQgPe&amp;sig=CXD6nKFndExsmkc_seBy9LZuV3k&amp;hl=en&amp;sa=X&amp;ved=0ahUKEwjztvaE8brOAhXB7hoKHcWGAJMQ6AEIXzAJ#v=onepage&amp;q=ruby%20grep%20hash%20key&amp;f=false\">search your hashes by grepping them<\/a>. Pretty cool stuff! It&#8217;s a little thing, but it&#8217;s dead useful in certain situations, like if you have formatted or parsed input coming in but don&#8217;t know exactly what to expect.<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<p>That&#8217;s it for now, but pushing yourself in the world of software engineering means you discover new challenges everyday &#8212; and need to solve them! So hang tight, and as always, thanks for reading!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I like to chronicle the little discoveries and revelations in my day&#8217;s work. It helps keep me clearheaded, gives me a reference for later, and lets me share my knowledge with the world. Granted, in large part, these &#8220;discoveries&#8221; have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":217,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[109,125,147],"tags":[67,113,168,167,166,37,70,72,84],"class_list":["post-216","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learning","category-programming","category-software-engineering","tag-coding","tag-development","tag-file-access","tag-grep","tag-hashes","tag-learning","tag-programming","tag-ruby","tag-strings"],"_links":{"self":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts\/216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/comments?post=216"}],"version-history":[{"count":2,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts\/216\/revisions"}],"predecessor-version":[{"id":219,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/posts\/216\/revisions\/219"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/media\/217"}],"wp:attachment":[{"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/media?parent=216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/categories?post=216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mberlove.com\/blog\/wp-json\/wp\/v2\/tags?post=216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}