Ruby like blocks in Cobra?
Posted: Mon Jul 13, 2009 11:14 am
Hi,
Recently Yehuda Katz: pointed out that one of the two main advantages Ruby has over Python is that Ruby can take blocks of code and that is heavily used in Rails. As far as I know, Cobra don’t have Ruby like blocks. So the suggestion is to introduce some Ruby like blocks, for instance, as shown below.
In v1 suggested example, ‘do’ appears in a different context (as in Ruby) but for the same purpose as when used in a closure or lambda. In v2 suggested example do is a “normal” "multi-line statement lamdbda".
It seems like VB 10 will get "multi-line statement lambdas" similar to Ruby blocks but slightly clumsier:
I hope it is possible to implement some version of blocks in Cobra. (The other main Ruby advantage over Python is simpler meta programming.)
Regards
Csaba
Recently Yehuda Katz: pointed out that one of the two main advantages Ruby has over Python is that Ruby can take blocks of code and that is heavily used in Rails. As far as I know, Cobra don’t have Ruby like blocks. So the suggestion is to introduce some Ruby like blocks, for instance, as shown below.
- Code: Select all
class TimingUsingClosureAndBlock
sig ParameterlessSubOrBlock
def timeIt(aProc as ParameterlessSubOrBlock) is shared
startTime= DateTime.now
print 'Start [ startTime]'
aProc.invoke
print 'Cobra elapsed time is [DateTime.now.subtract(startTime)] seconds'
print
def main
timeThis as ParameterlessSubOrBlock = do
# could be many statements here
for i in 0:1_000_000
x = Math.sin(Math.pi*i/2_000_000)
# This is legal Cobra code but requires that the code is in a closure
.timeIt( timeThis )
# v1 Suggested Ruby like block to achieve the same result
.timeIt.do
# could be many statements here
for i in 0:1_000_000
x = Math.sin(Math.pi*i/2_000_000)
# v2 Alternative Ruby like block
.timeIt(do
# could be many statements here
for i in 0:1_000_000
x = Math.sin(Math.pi*i/2_000_000)
In v1 suggested example, ‘do’ appears in a different context (as in Ruby) but for the same purpose as when used in a closure or lambda. In v2 suggested example do is a “normal” "multi-line statement lamdbda".
It seems like VB 10 will get "multi-line statement lambdas" similar to Ruby blocks but slightly clumsier:
- Code: Select all
animal.DoTrick(Sub()
animal.LegsOnGround = 0
Console.WriteLine("I'm Jumping now!")
animal.LegsOnGround = 4
End Sub)
I hope it is possible to implement some version of blocks in Cobra. (The other main Ruby advantage over Python is simpler meta programming.)
Regards
Csaba