Ruby Noobie needs some help

R

r

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

Robert Dober

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

Phlip

r said:
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...
 
T

Tim Hunter

r said:
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.
 
L

Louis-Philippe

[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.hobix.com/
http://poignantguide.net/ruby/

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



2008/12/30 Robert Dober said:
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???? did you mean function ?
Does anybody know of a good Python2Ruby.tut() ?? Not me unfortunately.
HTH
Robert
 
B

brabuhr

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"
 
N

Nicholas Wieland

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

r

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

r

r said:
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 awider
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 - Ithink
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 superiorto
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...

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

r

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

w_a_x_man

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top