Ruby Noobie needs some help

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...

testscript.function('hello')
testscript.cams[0]

I can also do "from testscript import cams, function" and then do

cams[0]
function('hello')

Does anybody know of a good Python2Ruby.tut() ??

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

methods defined with def are not closures.
Try this
module X
  cams = ...
  define_method :function do |s|
     puts s
     cams.each do | cam | print cam end
end
end

include X
function

This is however probably not what you want to do

test()

test??? did you mean function ?
<Python semantics snipped, but noted :wink: >

Does anybody know of a good Python2Ruby.tut() ??

Not me unfortunately.

HTH
Robert

···

On Tue, Dec 30, 2008 at 9:49 PM, r <rt8396@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...
> testscript.function('hello')
> 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
> cams[0]
> 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

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...
> testscript.function('hello')
> testscript.cams[0]

I can also do "from testscript import cams, function" and then do
> cams[0]
> 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/

Two problems:
  * test() is not defined anywhere?
  * The variables named "cams" in the function definition and outside
of it are local variables in different scopes. One option would be to
use an instance variable (@cams):

@cams = %w{ cam1 cam2 cam3 }
def function(s)
  puts s
  for x in @cams
    print x
  end
end
function("foo")
#=> "foo\ncam1cam2cam3"

···

On Tue, Dec 30, 2008 at 3:49 PM, r <rt8396@gmail.com> 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()

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

···

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...
> testscript.function('hello')
> testscript.cams[0]

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()

You define "function" but never use it.
You use "test" but never define it.

In python this is perfectly valid,

You assert that this is the Python way.
Keep looking over your shoulder for Guido.

Cams = %W(cam1 cam2 cam3)

def foo s
  puts s, Cams.join( ", " )
end

foo "think"

···

On Dec 30, 2:48 pm, r <rt8...@gmail.com> wrote:

While a python2ruby tutorial is not a bad idea, If you want to understand
ruby you have to learn to think like ruby,
and starting to learn ruby by comparison, specially to a very different
language, would be backward thinking a little.

I would recommend you start with pure ruby tutorial:
http://tryruby.hobix.com/
http://poignantguide.net/ruby/

if you like the language and want to learn it,
go for the pickaxe book:

···

2008/12/30 Robert Dober <robert.dober@gmail.com>

On Tue, Dec 30, 2008 at 9:49 PM, r <rt8396@gmail.com> 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
methods defined with def are not closures.
Try this
module X
cams = ...
define_method :function do |s|
    puts s
    cams.each do | cam | print cam end
end
end

include X
function

This is however probably not what you want to do

> test()
test??? did you mean function ?
<Python semantics snipped, but noted :wink: >
>
> Does anybody know of a good Python2Ruby.tut() ??
Not me unfortunately.
>
>
HTH
Robert

Robert,
Oops, Yea my script was suppost to look like this...

#testscript.rb
cams = ['cam1', 'cam2', 'cam3']
def function(s)
    puts s
    for x in cams;print x;end
end
function()

I really like the each() method, it saves eairly onset of Carpal
Tunnel :). I was not aware that you could leave out the brackets. C
and Python has made me too comfortable with for loops. I will look
into modules also.
Thanks

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

···

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...
> > testscript.function('hello')
> > 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
> > cams[0]
> > 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, Thanks everybody i am getting a good insight to how Ruby works
now. "each" instead of "for this in that:blah", "$" instead of
"global", "@" instead of "self". I am starting to like these
shortcuts. Ruby may be more worth while than i had initially thought.
i am beginning to believe :).
Thanks Robert, Philip, Tim, Luis, Brab, Nicholas.

When you'll want more,
the official Ruby website has plenty more for you in terms of docs:
http://www.ruby-lang.org/en/documentation/

even some answer to your first question :wink:
http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-python/

have a nice journey!

···

2008/12/30 r <rt8396@gmail.com>

OK, Thanks everybody i am getting a good insight to how Ruby works
now. "each" instead of "for this in that:blah", "$" instead of
"global", "@" instead of "self". I am starting to like these
shortcuts. Ruby may be more worth while than i had initially thought.
i am beginning to believe :).
Thanks Robert, Philip, Tim, Luis, Brab, Nicholas.