Example of the difference between Ruby and Java
Having worked extensively with both ruby and java, I thought I'd share some interesting differences between how one can approach solving problems in the two languages.
Let's take a common example of a function that will take a variable number of arguments and return a string with all non-null values separated by commas. We'll start by just using the core runtime and see what we can do.
Here's the Java example:
Now the equivalent in Ruby:
Ouch! It actually gets much better, because if you really need this sort of functionality a lot,you could just add the method directly to the Array class and do this in ruby:
The latter approach would simply be impossible in java, but with just a little extra effort, allows you to enhance your ruby Array class to give you some very useful functionality.
At it's core, ruby is geared toward rapidly producing compact code and enables developers to modify their environment to support just about anything they want to do. Java, on the other hand, is geared more toward following a lowest common denominator standard at the expense of being more verbose and not allow developers to expand the language to meet their needs.
I'm not suggesting ruby is superior in all cases, but from a flexibility and development effort perspective, there really isn't any fair comparison, ruby trounces java in almost every respect.
Let's take a common example of a function that will take a variable number of arguments and return a string with all non-null values separated by commas. We'll start by just using the core runtime and see what we can do.
Here's the Java example:
public class StringJava { public static void main(String[] args){ System.out.println(printComma("test1","test2","Test3")); System.out.println(printComma("test1",null,"Test3","Test4")); } public static String printComma(String... names) { StringBuffer buff = new StringBuffer(); boolean first = true; for (String str: names) { if (str != null) { if (first) { buff.append(str); first = false; } else { buff.append(','+str); } } } return buff.toString(); } }
Now the equivalent in Ruby:
def print_comma(*data) data.compact.join(',') end puts print_comma "Test1","Test2","Test3" puts print_comma "Test1",nil,"Test3","Test4"
Ouch! It actually gets much better, because if you really need this sort of functionality a lot,you could just add the method directly to the Array class and do this in ruby:
class Array def comma_join self.compact.join(',') end end puts ["Test1","Test2","Test3"].comma_join puts ["Test1",nil,"Test3","Test4"].comma_join
The latter approach would simply be impossible in java, but with just a little extra effort, allows you to enhance your ruby Array class to give you some very useful functionality.
At it's core, ruby is geared toward rapidly producing compact code and enables developers to modify their environment to support just about anything they want to do. Java, on the other hand, is geared more toward following a lowest common denominator standard at the expense of being more verbose and not allow developers to expand the language to meet their needs.
I'm not suggesting ruby is superior in all cases, but from a flexibility and development effort perspective, there really isn't any fair comparison, ruby trounces java in almost every respect.
Comments
I wish there was an easier way in Java to master the common example of taking a variable number of arguments and returning a string with all non-null values separated by commas! It might come in handy, one day!
function print_comma($array) {
return implode("," $array);
}
echo print_comma(array("Test1","Test2","Test3"));
echo print_comma(array("Test1",null,"Test3","Test4"));
import org.apache.commons.lang.StringUtils;
StringUtils.join(names, ',');
But why do you compare Ruby with Java? It makes more sense to compare Ruby with Python, Perl, Groovy...
string.Join(",", new string[] { "Test1", "Test2", "Test3" });
Console.Write(string.Join(",", new string[] { "Test1", "Test2", "Test3" }));
Developing your own library of tools should be compulsory to reach intermediate programmer. Senior programmers, we don't even need our libraries because we can code them ad-hoc with our right hand while our left hand is moving on to through the app logic.
-------
Another IT Blog
Try this in other platforms and aim for bulletproof runtime i.e WILL Not Throw Errors at RUNTIME.
string joinnedononnullstartwithF = string.Join(",", (from s in new[]{"First","Second","F string"} where !string.IsNullOrEmpty(s) && s.StartsWith("F") select s).ToArray())
void Main()
{
Console.WriteLine(string.Join(",", names.Where(n => n != null)));
}
For your second Ruby example, C# equivalent is:
static class Extensions
{
public static string CommaJoin(this IEnumerable<string> names)
{
return string.Join(",", names.Where(n => n != null));
}
}
void Main()
{
Console.WriteLine(new [] {"Test1", null, "Test3", "Test4"}.CommaJoin());
}
I usually have an StringJoin() extension method on IEnumerable<string>, so I can do this:
new[] {"test1", null, "Test3"}.Where(n => n != null).StringJoin(",");
Thanks for sharing.
Just because, a corrected PHP example:
echo implode (',', array_filter ([ '1', null, '3', '4' ]));
And a Javascript example as well:
[ '1', null, '3', '4' ].filter ((x) => x !== null).join (',')
Sorry I love one-liners :-)