From: Nicholas Wieland Date: 2008-12-31T08:50:24+09:00 Subject: Re: Ruby Noobie needs some help Il giorno 30/dic/08, alle ore 21:49, r ha scritto: > Hello, > I am coming from the python world and trying to learn ruby. Why does > this throw an error? > > #testscript.rb > cams = ['cam1', 'cam2', 'cam3'] > def function(s) > puts s > for x in cams > print x > end > end > test() > > In python this is perfectly valid, must i write a class for > everything? Also how does ruby handle modules compared to python? In > python i can do "import testscript" and access the function and array > as... > py> testscript.function('hello') > py> testscript.cams[0] Scoping in Python is different. You're trying to translate python 1:1, and you really shouldn't. If you need a function (aka a method of the Kernel class), declare stuff inside your method, or use a constant or class variable (uh, better you declare it inside the method, unless it's a throw-away script). About modules, they can act as simple namespaces, like MODULE::something, but i suggest you look at mixins. In Python mixins can be dangerous, I know. Now forget everything, in Ruby it's the opposite. Python permits a fully structural approach, while Ruby just makes it possible but clearly states that it's not his cup of tea. Have fun with Ruby. ngw