[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby Noobie needs some help

r

12/30/2008 8:49:00 PM

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]

I can also do "from testscript import cams, function" and then do
py> cams[0]
py> function('hello')

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

Robert Dober

12/30/2008 9:02:00 PM

0

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 ;) >
>
> Does anybody know of a good Python2Ruby.tut() ??
Not me unfortunately.
>
>
HTH
Robert

Phlip

12/30/2008 9:04:00 PM

0

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

Tim Hunter

12/30/2008 9:17:00 PM

0

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

Louis-Philippe

12/30/2008 9:26:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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....
http://poignantguide...

if you like the language and want to learn it,
go for the pickaxe book:
http://www.pragprog.com/titles/ruby/progra...



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 ;) >
> >
> > Does anybody know of a good Python2Ruby.tut() ??
> Not me unfortunately.
> >
> >
> HTH
> Robert
>
>

brabuhr

12/30/2008 9:26:00 PM

0

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

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"

Nicholas Wieland

12/30/2008 11:50:00 PM

0

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


r

12/31/2008 1:54:00 AM

0

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

r

12/31/2008 2:08:00 AM

0

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

r

12/31/2008 2:39:00 AM

0

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.

Louis-Philippe

12/31/2008 3:21:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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

even some answer to your first question ;)
http://www.ruby-lang.org/en/docu...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.
>
>