{"id":104,"date":"2008-05-01T23:35:39","date_gmt":"2008-05-01T18:05:39","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/?p=104"},"modified":"2008-05-01T23:35:39","modified_gmt":"2008-05-01T18:05:39","slug":"hello-script-for-ruby","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2008\/05\/hello-script-for-ruby\/","title":{"rendered":"Hello Script for Ruby"},"content":{"rendered":"<p><a href='http:\/\/www.bin-co.com\/blog\/wp-content\/uploads\/2008\/05\/ruby_logo.gif'><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.bin-co.com\/blog\/wp-content\/uploads\/2008\/05\/ruby_logo.gif\" alt=\"\" title=\"Ruby Logo\" width=\"105\" height=\"109\" align=\"right\" class=\"intro size-full wp-image-105\" \/><\/a><\/p>\n<p class=\"intro\"><a href=\"http:\/\/www.bin-co.com\/blog\/2008\/03\/learning-a-new-programming-language-the-hello-world-method\/\">Hello Script<\/a> is a <strong class=\"highlight\">file that contains the most commonly used elements of a programming language so that it can be used as a cheatsheet<\/strong> when working with that language. This Hello Script for Ruby is the sixth post in this series.<\/p>\n<p>I have some experience with Ruby. I will not call myself an expert &#8211; but I am comfortable with Ruby. <a href=\"http:\/\/binnyva.blogspot.com\/2005\/09\/learning-ruby.html\">I like ruby.<\/a> I even have written a few <a href=\"http:\/\/www.bin-co.com\/ruby\/tk\/locater\/\">applications<\/a> in it.<\/p>\n<h2>Hello Code<\/h2>\n<pre><code class=\"ruby\">\n#!\/usr\/bin\/ruby\n \nprint \"Hello World\\n\"\n\nname = \"Binny\"\nyear = 2008\nprint \"Hello, \" + name + \" - welcome to \" + year.to_s + \"\\n\"\n\nif (year &gt; 2008) then\n\tprint \"Welcome to the future - yes, we have flying cars!\"\nelsif (year &lt; 2008) then\n\tprint \"The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!\"\nelse\n\tprint \"Anything wrong with your time machine? You have not gone anywhere, kiddo.\"\nend\nprint \"\\n\\n\"\n\n# For loop like structure\n0.upto(3) { |i|\n\tprint i.to_s + \") Hi there!\\n\"\n}\nprint \"\\n\"\n\n# Numerical array\nrules = ['Do no harm', 'Obey', 'Continue Living']\ni = 0\nwhile i&lt;rules.length do\n\tprint \"Rule \" + (i+1).to_s + \": \" + rules[i] + \"\\n\"\n\ti = i+1\nend\nprint \"\\n\"\n\n# Associated arrays\nassociated = {\n\t'hello'\t=&gt;\t'world',\n\t'foo'\t=&gt;\t'bar',\n\t'lorem'\t=&gt;\t'ipsum'\n}\nassociated.each { |key,value|\n\tprint key + \" : \" + value + \"\\n\"\n}\nprint \"\\n\"\n\n# Using Join and Split\ncsv_values = \"hello,world,how,are,you\\n\".split(\",\")\nprint csv_values.join(\":\")\n \n# Function, argument, return, call\ndef hello(name)\n\treturn \"Hello \" + name\nend\nhello_string = hello(\"Binny\")\nprint \"Function call returned '\" + hello_string + \"'\\n\\n\"\n\n \n# One for the OOP fanboys - Class, members, object and stuff.\nclass Movie\n\tpublic\n\t@name = ''\n\t@rating = 0\n\t\n\tdef initialize(name)\n\t\t@name = name\n\t\tself.rateMovie()\n\tend\n\t\n\tdef rateMovie()\n\t\t@rating = (@name.length % 10) + 1 #IMDBs rating algorithm. True story!\n\tend\n\t\n\tdef printMovieDetails()\n\t\tprint \"Movie : \" + @name + \"\\n\"\n\t\tprint \"Rating : \" + '*' * @rating + \"(\" + @rating.to_s + \")\\n\\n\"\n\tend\nend\n# Create the object\nncfom = Movie.new(\"New Country for Old Men\") #It's a sequel!\nncfom.printMovieDetails()\n\n\n# File IO\n# File reading, easy method...\nfile_in = File.new('Hello.rb', 'r')\ncontents = file_in.read\nfile_in.close\nprint \"Hello has \" + contents.length.to_s + \" chars\\n\"\n\n# Writing to a file\nfile_out = File.new('\/tmp\/hello.txt', 'w')\nfile_out.print \"Hello World from Ruby.\"\nfile_out.close\n \n# Command Executing\nprint `ls` #Execute the command 'ls' and print its output\nprint \"\\n\"\n \n# Regular Expressions\nstring = \"Hello World\"\nprint \"Yup - its evil\\n\" if(\/^Hell\/.match(string))\nprint string.gsub(\/l([^l])\/, '\\1') #Remove an 'l' from both words. Should print 'Helo Word' - The second arg must be in single quotes\n\nprint \"\\n\"\n\n# Some special\/only-in-ruby stuff...\n# Using a library\nrequire \"fileutils\"\n\n#Using yield\/code blocks...\ndef doXTimes(i)\n\t0.upto(i) {|count|\n\t\tyield count+1\n\t}\nend\ndoXTimes(5) {|count| \n\tprint count.to_s + \"th Time\\n\"\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Hello Script is a file that contains the most commonly used elements of a programming language so that it can be used as a cheatsheet <a class=\"mh-excerpt-more\" href=\"https:\/\/www.bin-co.com\/blog\/2008\/05\/hello-script-for-ruby\/\" title=\"Hello Script for Ruby\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,25,28],"tags":[75,80,137,242,245],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-ruby","category-scripts","category-tutorial","tag-cheatsheet","tag-code","tag-hello","tag-ruby","tag-script"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}