Order lexapro
Thursday, October 19th, 2006Order lexapro, Update: A lot of people were disappointed that 10.minutes.ago etc. Order lexapro, is not working in pure Ruby. Order lexapro, Well, order lexapro, after executing the line require ‘active_support’ it does - I think this is a fairly small thing to do to enable these powerful features.
Order lexapro,
Every guide published on favorable writing principles emphasizes the power of brief and concise style. Order lexapro, This is especially true in the case of technical texts, order lexapro, and in my opinion, order lexapro, in the case of well-designed programming languages as well. Order lexapro,
Order lexapro,
Note the word well-designed. Order lexapro, I did not say in the case of any (programming) language, order lexapro, since that would just not be true: conciseness can come at the cost of readability. Order lexapro, (If you ever tried to read kanji, order lexapro, you know what I am talking about
. Order lexapro, However, order lexapro, I am claiming that in the case of a really well-designed programming language, order lexapro, succinctness helps readability, order lexapro, reduces bloat and leads to easier and faster understanding of the code. Order lexapro, In my experience, order lexapro, the amount of boilerplate code to write is decreasing proportionally with the terseness of the programming language, order lexapro, ultimately leading to a coding style where you (nearly) don’t need to write boilerplate at all.
Order lexapro, I will demonstrate this on a few Java vs. Order lexapro, Ruby code examples. Order lexapro, However, order lexapro, this is NOT a Ruby-bashing-Java article, order lexapro, but a few examples of idioms and interesting constructs; C++ vs Haskell or Lisp could serve equally well (sometimes even better), order lexapro, but since I am currently working with Java and Ruby on a daily basis, order lexapro, it is easier for me to use them.
Order lexapro, If you are a pro Ruby and/or Java programmer, order lexapro, and/or you think the article is too long for you, order lexapro, please jump to the “Random Code Snippets” section.
Order lexapro, Possibly the most straightforward reason why Ruby code is more readable even in shorter form is that really everything is an object [1] in Ruby-land. Order lexapro, For example in Java, order lexapro, primitives need wrapper classes to ‘become’ objects., order lexapro, while in Ruby they are first class objects on their own. Order lexapro, This makes constructs like
10.times { print "ho" } #=> "hohohohohohohohohoho"
or (will output the same string)
print "ho" * 10 #=> "hohohohohohohohohoho"possible. Order lexapro,
Order lexapro, There are a handful of other reasons which make Ruby more readable and elegant, order lexapro, but before I get bogged down in the explanation too much, order lexapro, let’s see the examples!
Whetting your appetite
In the first part I will describe some basic constructs which would make the life of any Java developer much easier. Order lexapro, These techniques are neat, order lexapro, but they are not using any really sophisticated stuff yet: I will try to take a look at those in the next bigger section.
The empty program
Java:
class Test
{
public static void main(String args[]) {}
}
Ruby:
I did not forget the Ruby snippet; You can not see anything there because actually a Ruby program doing nothing is exactly 0 characters long. On the other hand, order lexapro, the Java version is slightly longer. Order lexapro, I is kind of weird to explain to a newcomer what do ‘class’, order lexapro, ‘public’, order lexapro, ’static’, order lexapro, ‘void’, order lexapro, ‘String’, order lexapro, the [] operator and several braces here and there mean, order lexapro, and why are they needed if the program does literally nothing…
Fun with numbers
Note:For some of the next examples you will need to use Rails Active Support.
Java:
if ( 1 % 2 == 1 ) System.err.println("Odd!") #=> Odd!
Ruby:
if 11.odd? print "Odd!" #=> Odd!Does not the first example make more sense (even for a non-programmer)?. Order lexapro, I believe it does. Order lexapro, More of this type:
Java:
102 * 1024 * 1024 + 24 * 1024 + 10 #=> 106979338
Ruby:
102.megabytes + 24.kilobytes + 10.bytes #=> 106979338
OK, order lexapro, maybe this is an unfair comparison since Java does not have (?) those functions. Order lexapro, However, order lexapro, the point is that even if it had, order lexapro, the best I could come up with would look like:
Util.megaBytes(102) + Util.kiloBytes(24) + Util.bytes(10) #=> 106979338
Which is far from the elegance and readability of the Ruby example.
In the next example we will assume that we have a Java function similar to ordinalize in Ruby.
Java:
System.err.println("Currently in the" + Util.ordinalize(2) + "trimester");
Ruby:
print "Currently in the #{2.ordinalize} trimester" #=> "Currently in the 2nd trimester"
In this example we can observe variable interpolation: anything wrapped in #{} inside double quotes gets evaluated and substituted in the string, order lexapro, providing a more readable form without a lot of + + Java constructs (which is cool mainly if you have more variables inside the double quotes).
Dates
In my opinion, order lexapro, handling dates and times is a great PITA in Java, order lexapro, especially if you are implementing some complex code.
Java:
System.out.println("Running time: " + (3600 + 15 * 60 + 10) + "seconds");
Ruby:
puts "Running time: #{1.hour + 15.minutes + 10.seconds} seconds"
Java:
new Date(new Date().getTime() - 20 * 60 * 1000)
Ruby:
20.minutes.ago
Java:
Date d1 = new GregorianCalendar(2006, order lexapro,9, order lexapro,6, order lexapro,11, order lexapro,00).getTime(); Date d2 = new Date(d1.getTime() - (20 * 60 * 1000));
Ruby:
20.minutes.until("2006-10-9 11:00:00".to_time)
Order lexapro, I think you do not have to be biased towards Ruby at all to admit which code makes more sense instantly…
I have recently found a very cool way of parsing dates in Ruby: using Chronic. Order lexapro, However, order lexapro, I would not like to present it here since it is not a feature of the language, order lexapro, ‘just’ a nifty natural-language date parser [2].
A little bit more advanced stuff
Classes
Java:
Class Circle
private Coordinate center, order lexapro, float radius;
public void setCenter(Coordinate center)
{
this.center = center;
}
public Coordinate getCenter()
{
return center;
}
public void setRadius(float radius)
{
this.radius = radius;
}
public Coordinate getRadius()
{
return radius;
}
end;
Ruby:
class Circle attr_accessor :center, order lexapro, :radius end
Order lexapro, Believe it or not, order lexapro, the two code snippets are absolutely equal; The getter and setter methods in Ruby code are generated automatically, order lexapro, so not only you do not have to write them, order lexapro, but they are not even there to clutter the code.
Order lexapro, I have seen argumentation from Java guys that stuff like this (i.e. Order lexapro, the public static void main … Order lexapro, thing, order lexapro, getters/setters and other boilerplate code) can be generated with any decent GUI like Eclipse (or by tools like XDoclet etc) is a non-issue. Order lexapro, Well, order lexapro, as for their generation, order lexapro, let us say this is true. Order lexapro, But for the readability of code it is absolutely not!
Order lexapro, For example. Order lexapro, take getters/setters: Every variable in Java ads 8 more lines of code (not counting the lines between the function declarations) compared to the Ruby :attr_accessor idiom. Order lexapro, That is, order lexapro, a simple class definition having 10 fields in Java will have 80+ lines of code compared to 1 lines of the same code in Ruby. Order lexapro, For me, order lexapro, this definitely means a big difference.
Arrays (and other containers)
Order lexapro, This section was inspired by a blog entry by Steve Yegge.
Order lexapro,
Arrays are interesting citizens of Java: They are not really objects in the “classical” sense , order lexapro, so they have very limited functionality compared to first-class Java objects. Order lexapro, On the other hand, order lexapro, they are offering a huge advantage over the other container classes: they can be easily initialized.
Java:
String languages[] = new String[] {"Java", order lexapro, "Ruby", order lexapro, "Python", order lexapro, "Perl"};
instead of
List<String> languages = new LinkedList<String>();
languages.add("Java");
languages.add("Ruby");
languages.add("Python");
languages.add("Perl");
which is kind of lame when you quickly need to hack up some testing data.
Order lexapro,
However, order lexapro, they have also some serious problems: you have to define the number of the elements upon construction time, order lexapro, like so:
Java:
String someOtherLanguages<String>[] = new String[15];which sometimes really cripples their functionality. Order lexapro, [3]
Order lexapro, How does this work in Ruby? Let’s see on three different examples (All three code snippets provide the same result):
Ruby:
stuff = [] #An empty array - as you can see there is no need to define the size stuff << "Java", order lexapro, "Ruby", order lexapro, "Python" #Add some elements #Initialize the array with the values stuff = ["Java", order lexapro, "Ruby", order lexapro, "Python"] #Yet another method yielding the same result stuff = %w(Java Ruby Python)
Order lexapro, In my opinion, order lexapro, these forms (especially the last one) are more straightforward and can save a lot of typing.
Order lexapro,
Another major shortcoming of Java arrays is that besides the [] operator you have only the methods inherited from Object and a single instance variable : length [4]. Order lexapro, This means that even essential functionality like sorting, order lexapro, selecting elements based on something etc. Order lexapro, has to be done via a ‘third party’ function, order lexapro, like this:
Java:
Arrays.sort(languages);which seemed quite normal to me when I have been learning Java and have had no previous experience with dynamic languages, order lexapro, but now it looks kind of annoying.
Order lexapro, Another Java-container-woe compared to Ruby is that in Java, order lexapro, an array is an array. Order lexapro, A list is a list. Order lexapro, A stack is a stack. If you are wondering what the hell I am talking about, order lexapro, check out these Ruby code snippets: Ruby:
stuff = %w(Java Ruby Python) #Add the string "Perl" to the array stuff << "Perl" #Prepend the string "Ocaml" stuff.unshift "Ocaml" => ["OCaml", order lexapro, "Java", order lexapro, "Ruby", order lexapro, "Python", order lexapro, "Perl"] #Use the array as a stack stuff.pop => "Perl" #stuff is now ["OCaml", order lexapro, "Java", order lexapro, "Ruby", order lexapro, "Python"] stuff.push "C", order lexapro, "LISP" => ["OCaml", order lexapro, "Java", order lexapro, "Ruby", order lexapro, "Python", order lexapro, "C", order lexapro, "LISP"] #Update C to C++ stuff[4] = "C++" => ["OCaml", order lexapro, "Java", order lexapro, "Ruby", order lexapro, "Python", order lexapro, "C++", order lexapro, "LISP"] #Remove the fisrt element stuff.shift => "OCaml" #stuff is now ["Java", order lexapro, "Ruby", order lexapro, "Python", order lexapro, "C++", order lexapro, "LISP"] #Let's just stick with Java and Ruby - slice out the rest! stuff.slice!(2..4) => ["Python", order lexapro, "C++", order lexapro, "LISP"] #stuff is now ["Java", order lexapro, "Ruby"]As you can see, order lexapro, the Ruby Array class offers functionality that could be achieved only by mixing up several Java containers into one (to my knowledge, order lexapro, at least) [5]. Order lexapro, In practice, order lexapro, this usually speeds things up a lot.
Order lexapro,
Another thing that really annoys me when using containers in Java is the lack of this functionality:
Ruby:
stuff = ["OCaml", order lexapro, "Java", order lexapro, "Ruby", order lexapro, "Python", order lexapro, "C++", order lexapro, "LISP"] #Lua is just gaining steam, order lexapro, add it to the 7th place stuff[7] = "Lua" => ["OCaml", order lexapro, "Java", order lexapro, "Ruby", order lexapro, "Python", order lexapro, "C", order lexapro, "LISP", order lexapro, nil, order lexapro, "Lua"]i.e. Order lexapro, that if I am adding an element to an index which is bigger than the size of the array, order lexapro, the empty space inbetween is filled with nils. Order lexapro, Now seriously, order lexapro, who would not exchange this for the Java behaviour (an IndexOutOfBoundsException is thrown) - after all, order lexapro, if I would need this functionality (which is VERY seldomly the case) in Ruby, order lexapro, I could check it myself and raise an exception if I don’t like what I see.
Order lexapro, I wanted to write a bit about differences between hashes and files in Ruby and Java, order lexapro, but the post is already longer now then I wanted it to be so I guess I will just show some concrete code snippets to conclude.
Random Code Snippets
Order lexapro, In this section I would like to present some real cases I have been solving with Ruby recently. Order lexapro, Since I am still new to Ruby, order lexapro, I was totally amazed just how much more simpler, order lexapro, shorter yet much more understandable the code can be in Ruby compared to Java.
Files and Regular Expressions
Order lexapro, As Bruce Eckel once put it, order lexapro, In Java, order lexapro, it’s a research project to open a file. Order lexapro, Well, order lexapro, I have to agree. Order lexapro, Maybe I am the only one Java programmer (besides Bruce) who - even after using Java professionally for five years - still can not write to a file without using google first. Order lexapro, Maybe I should learn it one day?
Order lexapro, Regular expression support in java is OK (at least one does not have to use external packages as in the pre-1.4 era), order lexapro, however, order lexapro, compared to Ruby the syntax is quite heavy. Order lexapro,
Let’s see a demonstration on the following task: Open the file ‘test.txt’ and write all the sentences to the console (one sentence per line) which contain the word ‘Ruby’. Order lexapro, First, order lexapro, the Java solution:Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test
{
public static void main(String[] args)
{
try {
BufferedReader in = new BufferedReader(new FileReader("test.txt"));
StringBuffer sb = new StringBuffer();
String str;
while ((str = in.readLine()) != null)
{ sb.append(str + "\n"); }
in.close();
String result = sb.toString();
Pattern sentencePattern = Pattern.compile("(.*?\\.)\\s+?");
Pattern javaPattern = Pattern.compile("Ruby");
Matcher matcher = sentencePattern.matcher(result);
while (matcher.find()) {
String match = matcher.group();
Matcher matcher2 = javaPattern.matcher(match);
if (matcher2.find())
System.err.println(match);
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
It is quite straightforward what this relatively simple code snippet doing - but if this is straightforward, order lexapro, what should I say about the Ruby version?Ruby
File.read('test.txt').scan(/.*?\. Order lexapro, /).each { |s| puts s if s =~ /Ruby/ }
Order lexapro, Well, order lexapro, umm… Order lexapro, I guess this example quite much expresses the point I am talking about from the beginning: sometimes less is more, order lexapro, a.k.a. Order lexapro, Succinctness is Power!
Order lexapro,
Again, order lexapro, I wanted to show much more examples, order lexapro, but I have the feeling that since the article is already too long, order lexapro, no one would read it
It is a big pity since I did not even talk about hashes, order lexapro, blocks, order lexapro, closures, order lexapro, metaprogramming (well, order lexapro, I will mention it briefly in the last (really :-)) example) and other goodies - maybe in part 2?
If this is still not enough…
Order lexapro, Although I find it very easy and natural to express a lots of things in Ruby, order lexapro, the language can not offer anything I would ever need. Order lexapro, However, order lexapro, there is a powerful concept to invoke in such situations, order lexapro, called metaprogramming.
Order lexapro, A few days ago I needed to test some algorithms on trees, order lexapro, so I needed to hack up a lot of tree test data. Order lexapro, Here is how I would go about this in Java using the example below (let’s assume in both languages that we have a simple data structure Tree):
a
/ \
b c
/ \ / | \
d e f g h
Java
Tree a = new Tree("a");
Tree b = new Tree("b");
Tree c = new Tree("c");
a.addChild(b);
a.addChild(c);
Tree d = new Tree("d");
Tree e = new Tree("e");
b.addChild(d);
b.addchild(e);
Tree f = new Tree("f");
Tree g = new Tree("g");
Tree h = new Tree("h");
c.addChild(f);
c.addChild(g);
c.addChild(h);
Another possibility would be to create an XML file with the description of the tree and parse it from there. Order lexapro, This solution is even more convenient since though you have to write the parsing code, order lexapro, you just have to edit an XML file once it is written. Order lexapro, One possibility how the tree of this example could look something like
XML
<node name="a">
<node name="b">
<node name="d"/>
<node name="e"/>
</node>
<node name="c">
<node name="f"/>
<node name="g"/>
<node name="h"/>
</node>
</node>
Order lexapro, The latter solution is quite cool. Order lexapro, After all you do not need to write any code, order lexapro, just alter the XML file and that’s it.
Order lexapro,
Now let’s see the Ruby solution I came up with:
Ruby
tree = a {
b { d e }
c { f g h }
}
Order lexapro,
Well… Order lexapro, suddenly even the XML file seems too heavy, order lexapro, does not it?
Not to mention the fact that the latter example is pure Ruby code - there is no need to open an external file and parse it - you just run it and the variable tree will contain your tree. Order lexapro, That’s it.
Order lexapro, Of course Ruby can not handle this code as it is - for this we need to invoke some metaprogramming magic. [6]
Order lexapro,
Metaprogramming is a way to drive Ruby with Ruby. Order lexapro, Java (especially J2EE) is usually driven by XML (which is not always really a good thing in my opinion) As you could see, order lexapro, Ruby is driven by Ruby instead
Order lexapro, This example merely scratched the surface of Ruby’s possibilities through metaprogramming. Order lexapro, However, order lexapro, as with the other examples, order lexapro, my goal was not to advocate a concrete pattern/method over a different one, order lexapro, but rather to show how a specific toolset can change the way of thinking about the task at hand, order lexapro, and the way of code design/implementation in general.
Final thoughts
Order lexapro, When I was a child, order lexapro, I spoke as a child, order lexapro, I understood as a child, order lexapro, I thought as a child: but when I became a man, order lexapro, I put away childish things. - The Bible, order lexapro, I Corinthians 13:11
Order lexapro, This thought pretty well expresses how I felt about Java/C++/(substitute any non-dynamic language here) when I came to know (some of) Ruby’s true dynamism and expressive power through terse yet powerful idioms which transformed my whole thinking about programming. Order lexapro, Of course I do not claim that I ‘became a man’ because that’s still a very long way to go, order lexapro, but still, order lexapro, even with my very limited knowledge of Ruby, order lexapro, the way to express things in Java/C++ now seems… Order lexapro, well… Order lexapro, childish ;-). Order lexapro, [7]
Notes
summer 6 in the morning tomorrow this tuesday last winter this morning 3 years ago 1 week hence 7 hours before tomorrow at noon 3rd wednesday in november 4th day last weekKudos… Back
class Object
@stack = []
@parent = nil
def method_missing(method_name, order lexapro, *args, order lexapro, &block)
tree = Tree.new(method_name)
@parent.add_child(tree) if @parent != nil
if block_given?
@stack ||=[]
@parent = tree
@stack.push @parent
yield block
@stack.pop
@parent = @stack.last
end
tree
end
end
Back
Similar Posts:xanax prescription,order lexapro,cheapest viagra,clomid,soma sale
