{"id":141,"date":"2006-07-23T22:59:17","date_gmt":"2006-07-24T03:59:17","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/07\/23\/ruby-language-goodies-for-java-developers\/"},"modified":"2012-06-29T19:45:18","modified_gmt":"2012-06-30T02:45:18","slug":"ruby-language-goodies-for-java-developers","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2006\/07\/23\/ruby-language-goodies-for-java-developers\/","title":{"rendered":"Ruby Language Goodies For Java Developers"},"content":{"rendered":"<p>If you are new to the Ruby programing language you might not be familiar with some of the short semantic sugar or language goodies that Ruby provides.  I think that for most of the hard core Java developers the code samples that will be provided here will be novel but Perl hackers will not be impressed.  So as an aid to all my fellow Java developers I will try to compare and contrast Ruby code with Java.  To get started lets set three values to three variables in one line of Ruby code:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nvar1, var2, var3 = 'one', 'two', 'three'\r\n<\/pre>\n<p>You can&#8217;t do this in Java.  The best you can do in Java is set three variables with the same value, which Ruby also supports, like this:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nvar1 = var2 = var3 = 'same value'\r\n<\/pre>\n<p><b>Methods<\/b><br \/>\nIn Ruby, you can initialize default values to method parameters.  Here is an example of a method with three parameters initialized to a numeral literal, empty list, and empty hash.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\ndef my_method(parama = 1, paramb = &#x5B;], paramc = {})\r\n  # method implementation here\r\nend\r\n<\/pre>\n<p>To invoke my_method you can do so with any of the following lines of code:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nmy_method\r\nmy_method()\r\nmy_method(2)\r\nmy_method(2, &#x5B;:abc, :def])\r\nmy_method(2, &#x5B;:abc, :def], :abc =&amp;gt; 'abc', :def =&amp;gt; 'def')\r\nmy_method 2, &#x5B;:abc, :def], :abc =&amp;gt; 'abc', :def =&amp;gt; 'def'\r\nmy_method 2, &#x5B;:abc, :def], {:abc =&amp;gt; 'abc', :def =&amp;gt; 'def'}\r\n<\/pre>\n<p>This is not method overloading as seen in Java.  All of the above method calls invoke the same block of code, the same method implementation.  In Java you would define overload the method by defining a separate implementation for each method signature.<\/p>\n<p>Java 1.5 recently introduced variable length arguments, <a href=\"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/02\/02\/java-five-oh-3-variable-arguments\/\">varargs<\/a>.  You could have done this in Ruby years back.  To define a Ruby method with variable number of parameters you can do the following:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\ndef my_vararg_method *params\r\n  # params is an array\r\nend\r\n<\/pre>\n<p>To invoke my_vararg_method you can do so with any of the following lines of code:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nmy_varargs_method # params is empty\r\nmy_varargs_method 'string', :symbol, {} # params = &#x5B;'string', :symbol, {}]\r\n<\/pre>\n<p>At this time I invite you to notice that parentheses, like semi-colons, are not required.<\/p>\n<p><b>Objects<\/b><br \/>\nIn Java you have two types of data, objects and native.  An int is completely different than an instance of Integer and depending on the situation you had to constantly convert one to the other.  In  Ruby everything is an object, and when I say everything I mean everything including the nil value.  The nil value is Ruby&#8217;s equivalent to the Java null keyword.  In Ruby you can do the following:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\n1.nil? # false\r\nnil.nil? # true\r\n1.3.class # Float\r\n1.is_a? Float # false\r\n'0'.to_i + 1 # 1\r\nFloat.class # Class\r\n<\/pre>\n<p>And because in Ruby everything is an object you don&#8217;t need a autoboxing hack to use the literal 1 as a key in a hash.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nhash = {1 =&amp;gt; 'one', 2 =&amp;gt; 'two', 3 =&amp;gt; 'three'}\r\n<\/pre>\n<p><b>Lists<\/b><br \/>\nAnother convenient language goodie available in Ruby is the array initializer.  If you want to construct an array of string values without white spaces in them you can quickly do so with the following statement:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nlist = %w(female male) # list = &#x5B;&quot;female&quot;, &quot;male&quot;]\r\n<\/pre>\n<p>If you need a whitespace inside a word just escape it with the backslash (\\).  Now If you want to define an array with the first 10 digits you can easily do so with ranges.  Here is an example of how to construct an array composed of the numbers between 0 to 9, inclusive.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nnums = (0 .. 9).to_a\r\n<\/pre>\n<p>Here is how to iterate through each letter in the alphabet using ranges.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\n('A' .. 'Z').each { |char| puts char }\r\n<\/pre>\n<p>If you want to loop over an array you will need a range from 0 to the array size, exclusive.  To create an exclusive range use the &#8230; operator as in the following:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\narr =  get_list # some array\r\n(0 ... arr.size).each { |index| print arr&#x5B;index] }\r\n<\/pre>\n<p><b>Control<\/b><br \/>\nTo wrap let me walk through some Ruby control constructs.  In addition to the control construct that Java developers are familiar with Ruby provides a unless and until.  The &#8216;unless condition&#8217; is equivalent to &#8216;if not condition.&#8217;  The &#8216;until condition&#8217; is equivalent to &#8216;while not condition.&#8217;  Here is a trivial example of the the unless construct.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nunless item.invalid?\r\n  print item.to_s\r\nend\r\n<\/pre>\n<p>Ruby also provides a shortcut for the above code in the form of conditional modifiers.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nprint item.to_s unless item.invalid?\r\n<\/pre>\n<p>In both of the above code snippets the item will only be printed if the item is in a valid state.  You can use if, unless, while, and until as  conditional modifier.<\/p>\n<p>As a bonus, let me state where Ruby is lacking.  The Ruby language does not support the &#8212; and ++ operator.  The best you can do in Ruby is use the next method available to the Integer class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are new to the Ruby programing language you might not be familiar with some of the short semantic sugar or language goodies that Ruby provides. I think that for most of the hard core Java developers the code samples that will be provided here will be novel but Perl hackers will not be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,22,3],"tags":[802,745,748,806,746,747],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-2h","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/141"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=141"}],"version-history":[{"count":2,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":1633,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/141\/revisions\/1633"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}