On Dec 30, 3:03 pm, Phlip <phlip2...@gmail.com> wrote:
> 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()
>
> Where is function 'test()'?
>
> (Note that my e-mails use 'single ticks' for code samples - don't type the ticks!)
>
> Next, I think that 'function()' cannot see 'cams' because it's in the wrong
> scope. If you want it to be constant, capitalize it, 'Cams', so it gets a wider
> scope, and the called function can see it.
>
> > In python this is perfectly valid, must i write a class for
> > everything?
>
> I direct your attention to the myriad Ruby tutorials, which exclaim proudly that
> - unlike Java - you don't need to write any class if you don't want one.
>
> (Ruby is still fully OO - the trick is you are inside a default class - I think
> it's 'Kernel'.)
>
> > 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]
>
> You 'require "testscript"', and your 'function()' is available without
> decoration. If you want it, you put 'module Testscript'
>
> > I can also do "from testscript import cams, function" and then do
> > py> cams[0]
> > py> function('hello')
>
> Then you would 'include Testscript' somewhere in the including module.
>
> From here, please crack a book. You will find Ruby infinitely superior to
> programmer-hostile systems like Java or Perl, and you will find it competes,
> feature-by-feature, with irritating systems like Python...
>
> > Does anybody know of a good Python2Ruby.tut() ??
>
> Did Google help? I know there's a Java-to-Ruby tutorial out there...
>
> --
> Phlip
OK, my problem is scoping. I need to learn about Ruby's scoping rules
but i have not found a tut that explains it clearly. I am really
comfortable with the Module.method or Module.attribute syntax and it
helps with name conflicts also. i need to find out the difference
between "include" and "require".
Thanks