Sorry, no podcast this week because I'm at the Dynamic Languages World Conference, where yesterday I managed to sit in on a Groovy Tutorial. Groovy, in case you're not aware, is "an agile and dynamic language for the Java Virtual Machine which builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk".
I'm not writing this article to portray Ruby in a negative light, I love Ruby. However, there were a few scoops of syntactic sugar which were contained in groovy which I thought were worth sharing.
1 2 3 4 5 6 |
def name = ["first_name":"Gregg", "last_name":"Pollack"] println name["first_name"] >> Gregg name.first_name >> Gregg |
Sugar: You can automatically access the hash keys as attributes just using .
Sugar: Some people might say that using : is simpler then =>
Sugar: Think about this as respect to mock objects, instead of:
1 2 3 |
user = mock('User') user.stub!(:save).and_return true user.stub!(:destroy).and_return false |
you could have
user = ["save":true, "destroy":false] |
that's hawt
1 2 3 |
def say_hello = { println "hello" } say_hello() >> "hello" |
To do the same thing in Ruby:
1 2 |
say_hello = lambda { puts "hello" }
say_hello.call |
Sugar: Kinda nice that you don't have to use lambda in Groovy.
Sugar: You also don't have to use ".call", you just use ().
1 2 |
[1,2,3].each { print it } >> 123 |
Sugar: In a closure, if you don't specify a parameter name, "it" is used. As opposed to Ruby:
1 2 |
[1,2,3].each { |it| print it } >> 123 |
Why do we have to specify the name of the parameter? It'd be nice to have a default like "it".
1 2 |
["gregg", "pollack"]*.toUpperCase() >> ["GREGG", "POLLACK"] |
Sugar: They have an operator for invoking methods on each item, the equivalent in Ruby:
1 2 |
["Gregg", "Pollack"].collect(&:upcase) >> ["GREGG", "POLLACK"] |
Just as good, but it's kinda neat they have an operator for that.
1 2 3 4 5 6 7 |
name1 = ["first_name":"Gregg", "last_name":"Pollack"] name2 = ["first_name":"Jason", "last_name":"Seifer"] names = [name1, name2] names.first_name >> ["Gregg", "Jason"] |
Sugar: Check that out, if you call for an attribute on an array, it will look for those attributes on the objects inside the array. This got me thinking, what happens if you call for an attribute that doesn't exist.
1 2 |
names.middle_name >> [null, null] |
hah... rad.. no error, just doesn't have that property. This could be sweat for an array of different types of objects. Now in Ruby:
1 2 3 4 5 6 7 |
name1 = {:first_name => "Gregg", :last_name => "Pollack"}
name2 = {:first_name => "Jason", :last_name => "Seifer"}
names = [name1, name2]
names.collect {|name| name[:first_name] }
>> ["Gregg", "Jason"] |
Hmm... I wonder if we can shorten that
1 2 |
names.collect(&:first_name) >> NoMethodError: undefined method `first_name' for {:last_name=>"Pollack", :first_name=>"Gregg"}:Hash |
Nope.. that's right, we don't want to call the method, we want to look up the value based on the key. Darn.
Sugar: I know there's some good reason why we can define all the normal operators (ex. +, -) but not ++. Groovy can define ++, and I'm kinda jealous. I miss ++.
I stole my wife’s Flip Video for this trip, so I decided to try my hand at doing some Video Blogging. I’ll be trying to post an update every day for the next crazy week here in Germany, and then back at Railsconf.
This first video isn’t very educational, FYI, but over the next few days as Steven and I run into Speakers we’ll be getting many interviews where we ask “Give us your talk in 30 seconds”.
If you want to catch all the videos feel free to subscribe to my Vimeo RSS Feed.
If you’re coming to Railsconf please do say hi. Here’s what I’ll be doing:
The videos we show at Railsconf will be posted here on the blog on Tuesday
No comments yet.
You must be logged in to add your own comment.