From: Tim Hunter Date: 2008-12-31T06:17:01+09:00 Subject: Re: Ruby Noobie needs some help r wrote: > 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? No. Here's the Ruby version: $cams = ['cam1', 'cam2', 'cam3'] def test(s) puts s $cams.each {|x| print x} end test("hello!") I made a couple of assumptions about your program, namely that you intended to name the function "test", not "function", and that you intended to pass it an argument. If you want to define test such that it can take an argument or not, then you could define it with a default argument like this: def test(s=nil) puts s $cams.each {|x| print x} end Note that in Ruby global variables are declared with a $ prefix. 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] > > I can also do "from testscript import cams, function" and then do > py> cams[0] > py> function('hello') > Ruby has the "require" statement. I don't know enough Python to tell you how different it is from import. -- RMagick: http://rmagick.rubyforge.org/