DCSIMG
Dynamic Languages and The .Net Framework - Promo #3 - IronShay

Dynamic Languages and The .Net Framework - Promo #3

This is the 3rd promo post to my session at Developer Academy III. Read the rest of the series:

This time I'd like to introduce you with 2 language specific possibilities that will demonstrate how intuitive these languages are...

Creating Enumerable Classes

Let's think of C# enumerators. In order to create objects that support enumerations, you'll have to inherit from IEnumerable and implement an IEnumerator object as well. You can also use yield for that (a post I've written about that can be found here) which simplifies the things a bit.
Now, close your eyes and try to think of a more intuitive way for doing so......................
Good, so what have you come up with? you can add a comment with your ideas.

Ruby has it figured out - just override the Each method and you're good to go... As simple as that!

class MyEnumerableClass
    def initialize(max)
        @max = max
    end
    
    def each
        (1..@max).each { |i| yield i }
    end
end

my_class = MyEnumerableClass.new(8)
my_class.each { |num| puts num }

This code will end up printing the numbers 1 to 8 on the screen.

Breaking From a Loop

The Python guys have realized that one of the major things people do with loops is to search for things in a collection. So we've ended up with a possibility to know that our loop has finished because it went through all of the items in the collection, without breaking away.

for i in nums:
    if i == 10:
        break    
else:
    print "10 wasn't found"
In this example, if nums contains 10, the code will continue. If nums doesn't contain 10, "10 wasn't found" will be printed to the screen.

Haven't had enough? come to my session at Developer Academy III! :)

All the best,
Shay.

Published Saturday, November 22, 2008 6:31 AM by shayf

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: