From: Christopher Dunn Date: 2006-03-02T10:47:46+09:00 Subject: Re: Torn in two - Pythonist ------=_Part_1031_1444751.1141264050603 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Reference: http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/178606 I'm a recent convert myself. I was convinced by this blog that I found via O'Reilly: http://www.oreillynet.com/ruby/blog/2006/01/bambi_meets_godzilla.html I will finish a project at work, where I use a powerful, pre-existing Pytho= n interface to a C++ library. Then, sadly, I'm done with Python (as soon as Ruby2.0 is out, fixing some annoyances). The best place to look is The Ruby Way. There is a 20-page appendix for Python converts, and another for Perl. Also read the 50 pages of Chapter 1, especially the gotchas under "Things to Remember". That's really all you need, coming from either Python or Perl. (It will take some time to understand blocks and continuations, but you don't have to use them.) There is a list of some differences with Python in [ruby-talk:*01528*] *rub= y <=3D> python* http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/1528 By far, the biggest gotcha is that 0 does not equal false. Another is that some mutating functions return nil when they change nothing, so you get unexpected exceptions from strung-together expressions like: foo().gsub!().bar() The number of similarities is amazing, partly because Ruby and Python borro= w from similar sources, and sometimes each other. E.g. * raise RuntimeError, msg * yield * def/class * for i in list * [1,2,3] * *args (and keyword args in Ruby2.0) * default function parameters * '.' for method calls I've put together my own table of some differences. Semicolons indicate related lines. Otherwise, separate statements represent other ways to do th= e same thing. (Hopefully, the HTML formatting works.) Python Ruby type(42) -> 42.class -> Fixnum 1e6 1.0e6 type(1e6) -> 1.0e6 # not 1.e6 1.0e6.class -> Float "Hello, %s" %(name) 'Hello, %s' %s(name) type('') -> "Hello, #{name}" # not single quotes ''.class -> String "Hello\\backslash \n newline" 'Hello\\backslash \n no newline' print func(x) puts func x puts(func(x)) puts func(x) puts(func x) a =3D 4 A =3D 5 a =3D 4 # lower case class C: pass class c: pass class C end # upper case NOT_CONST =3D 5; NOT_CONST =3D 6 CONST =3D 5; CONST =3D 6 -> warning (upper case) var global: $var instance: @var classvar: @@var None nil False, None, [], {}, 0, '' false, nil, FALSE, NIL # all others are true arr =3D [] arr =3D list() arr =3D () arr =3D tuple() arr =3D [] arr =3D Array.new # no tuples, but ... arr =3D [1,2,3].freeze assoc_arr =3D {} assoc_arr =3D dict() assoc_arr =3D {} assoc_arr =3D Hash.new a =3D {}; a[42] -> IndexError a =3D {}; a[42] -> nil a =3D Hash.new(2001); a[42] -> 2001; a.keys -> [] if x: do_something() elif y: do_something_else() else: do_anything() if x # no colons do_something elsif # Note spelling do_something_else else do_anything end # Must have do_something if x # no 'end' here while x and y: do_something while x and y do_something end begin do_something end while false # Here, do_something occurs once! # (Matz deprecates this usage.) do_something while false # Here, do_something never happens. result =3D os.popen(command).read() result =3D `#{command}` results =3D IO.popen(command, 'r').gets re.compile(stuff) Regexp.new(stuff) /stuff/ %r{stuff} regex.search(s) s ~=3D regex # returns position # Note: 0 is not false return x return x x # 'return' is not needed class Dog(Animal, Friend): normal =3D 0 def __init__(self): self.happiness =3D Dog.normal def wag(self, speed): self.happiness =3D speed class Dog < Animal @@normal =3D 0 def initialize() @happiness =3D @@normal end def wag(speed) @happiness =3D speed end end try: try: do_it() finally: undo_it() except RuntimeError, exception: print exception except: print "Unexpected" else: print "No exception raised." begin do_it() rescue RuntimeError =3D> exception puts exception rescue exc puts "Unexpected", exc else puts "No exception raised." ensure undoit() end ------=_Part_1031_1444751.1141264050603--