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.

Working with hashes (Maps)

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

Closures

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".

Working with arrays

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.

Fetching attributes of arrays

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.

They can define the ++

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 ++.

Vlogging and Railsconf

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:

  • Friday, 4:25 PM - 5:15 PM - Podcasting & Screencasting in Rails Panel, I’ll be talking with a few other Podcasters about how they put together their media.

  • Friday, 7:40 PM - 8:00 PM - Ruby Hero Awards Ceremony Before DHH’s keynote we’ll be giving out awards to people in our community who deserve some recognition.

  • Friday, 7:59 PM - We will premiere FOUR NEW RAILS ENVY VIDEOS before each keynote starting with DHHs. This time around we invited Five Runs Developer Adam Keys to join us in the madness.

  • Friday, 9:00 PM - 10:00 PM - Podcast Meetup If you can’t make our talk earlier in the day, have additional questions from the talk, or just want to meet other people who are passionate about Podcasting, come out to our birds of a feather.

The videos we show at Railsconf will be posted here on the blog on Tuesday

AddThis Social Bookmark Button Tags: ruby Ruby  | permalink

Blog

Subscribe to RSS Feed SubscribeRSS

Podcast

SubscribeRSS
iTunes
Archive

Ads from Ruby Row

   

Contact Us

Like what you see? Contact us today to get the ball rolling on your next great idea.

(407) 574-8059


Hosting by Rails Machine