{"id":272,"date":"2009-07-28T14:15:42","date_gmt":"2009-07-28T13:15:42","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=272"},"modified":"2009-07-28T14:15:42","modified_gmt":"2009-07-28T13:15:42","slug":"methods-arrays-hash","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/28\/methods-arrays-hash\/","title":{"rendered":"Methods, Arrays, Hash"},"content":{"rendered":"<p>First part is the method and the second is the array and hash variables.<\/p>\n<p>The manor in which Ruby returns values from a method is either by the return keyword or by the last executed line of code, thus if the result for the return of the method is the last line then there is no need to use the return keyword, but of course if the returnable value is calculated within the middle of the method and the rest is to clean up the method then use the return keyword.<\/p>\n<pre lang=\"ruby\">\r\n# this is a basic method\r\ndef saySomething(name)\r\n       result = \"hi there #{name}\"\r\n       return result\r\nend\r\n\r\n# but Ruby will return the last evaluated line, thus this method\r\n# is the same as the above one.\r\ndef saySomething2(name)\r\n       \"hi there #{name}\"\r\nend\r\n\r\n# the two methods that are the same in there results\r\nputs saySomething(\"Ian\")\r\nputs saySomething2(\"Ian Porter\")\r\n<\/pre>\n<p>Below is how Ruby uses the array structure and also the hash structure, an array is defined within the [ ] brackets and the start of the array is 0, but within a hash since there is no start to the hash keys, then there is an hash key and then the values associated with that key.  The hash structure is defined within { } brackets. <\/p>\n<pre lang=\"ruby\">\r\n# this is a array\r\na = ['Gentoo Linux','is','great']\r\n\r\n#this is a hash, an hash is a key -> value\r\nhas = { 'Gentoo' => 'Gentoo Linux',\r\n       'RH' => 'Red Hat Linux'}       # hash\r\n\r\nputs a[0]       # print the first element of the array\r\nputs a              # print the arary\r\n\r\nputs \"The Key value of RH within the hash\"\r\nputs has['RH']       # print the value assoicated with the key RH\r\nputs \"The hash in full\"\r\nputs has\r\n<\/pre>\n<p>Once executed from the console, the output is <\/p>\n<pre class=\"consoleoutput\">\r\nhi there Ian\r\nhi there Ian Porter\r\n<\/pre>\n<p>for the first method demonstrating that the result value is the same<\/p>\n<pre class=\"consoleoutput\">\r\nGentoo Linux\r\nGentoo Linux\r\nis\r\ngreat\r\nThe Key value of RH within the hash\r\nRed Hat Linux\r\nThe hash in full\r\nRHRed Hat LinuxGentooGentoo Linux\r\n<\/pre>\n<p>Which shows the results of the program, this shows that the two methods return in the same manor and also an array is broken into separate blocks, but a hash has a continuous memory block in nature.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First part is the method and the second is the array and hash variables. The manor in which Ruby returns values from a method is either by the return keyword or by the last executed line of code, thus if the result for the return of the method is the last line then there is &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/28\/methods-arrays-hash\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Methods, Arrays, Hash<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[8,398,404,400],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-ruby","tag-gnu","tag-linux","tag-ruby","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":273,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/272\/revisions\/273"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}