Ruby-Like Syntax in REALbasic (or RB has a C# 3.0 feature)
REALbasic, Software Development Add commentsI came across this post the other day (Ruby-Like Syntax in C# 3.0) and it make me think of REALbasic. The author talks about using the new C# 3.0 extensions methods capability to simulate Ruby syntax.
In particular this Ruby example is given:
20.minutes.ago
Which returns the time from twenty minutes ago. It’s a nice, readable syntax (if you know english).
It’s cool that C# 3.0 will have this (when it ships), but REALbasic has had extension method capability for years. I believe Extension methods in REALbasic were added around the 5.0 release which was in 2003, if I recall correctly.
Anyway, to do this in REALbasic you would add two public methods to a module:
Function Minutes(Extends minutes As Integer) Return Double Return minutes*60 End Function Function Ago(Extends seconds As Double) Return Date Dim now As New Date now.TotalSeconds = now.TotalSeconds - seconds Return now End Function
That’s it! Now you can call this like so to show the time from twenty minutes ago in a MessageBox:
Dim twenty As Integer = 20 MsgBox twenty.Minutes.Ago.LongTime
This has a limitation that Ruby and C# 3.0 don’t have: it won’t work with an integer literal. So, 20.Minutes.Ago won’t actually work, but really how often would you actually do it that way? But as a bonus, unlike C# 3.0, you don’t need to include parenthesis with extension methods that don’t have any parameters.
So, to recap:
Ruby:
20.minutes.ago
C# 3.0:
20.Minutes().Ago()
REALbasic:
twenty.Minutes.Ago or twenty.minutes.ago
Java:
new Date(new Date().getTime() - 20 * 60 * 1000);
So rather than learning Ruby or waiting around for Orcas and C# 3.0, you can use this syntax today with REALbasic.
Recent Comments