Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Python-tut] Conversions of Ruby-tut code
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Sat Jun 26, 2004 12:16 am   Post subject: [Python-tut] Conversions of Ruby-tut code

The first post here will be a conversion of the code from my "Hello, Ruby!" tutorial. I'll be sticking with the most basic version, since Python lacks a lot of the "nifty" features Ruby has in abundance. Of course, in all fairness, it also has a lot of nifty tricks Ruby lacks.


In Ruby
code:
puts "Hello, Ruby!"

print "Hello, Ruby!"


In Python
code:
print "Hello, Python!"

import sys
sys.stdout.write("Hello, Python!")


In Ruby
code:
puts "Hello, Ruby!"
print "I'm "
name = gets
puts "Ruby says, \"hello, #{name}\""


In Python
code:
import sys

print "Hello, Python!"
sys.stdout.write("I'm ")
name = sys.stdin.readline()
name = name.strip()
print "Python says, \"hello, %(name)s\"" % {"name": name}


Compare and Contrast

Python's "print" statement is roughly equivalent to Ruby's "puts" method, but adds an extra newline to any string. It should also be noted that "print" in Python is a language construct, while "puts" and "print" in Ruby are merely methods which can be dealt with in the same way as any other method.

code:
sys.stdout.write("I'm ")


Isn't as bad as it looks. It's essentially what we're really doing in Ruby with "print", though Ruby's equivalent to sys.stdout is the global variable $stdout or the constant STDOUT. We could rewrite the Ruby code as:

code:
$stdout.puts "Hello, Ruby!"
$stdout.print "I'm "
# or even $stdout.write "I'm "


Also a point of interest is that Ruby's methods may look like language constructs because they can be called with or without parentheses. Parentheses may be considered confusing for simple examples, or they may be required to clarify more complicated code.

When a method takes no arguments, though, there's absolutely no reason to include parentheses. Certainly "name.strip" is a bit easier on the typing fingers than "name.strip()".

It should also be noted that Python lacks a syntactic shortcut to get input from the user. The Ruby version of the Python way would be:

code:
name = $stdin.gets
# or name = $stdin.readline


The method used to insert the variable "name" into the string at the end of the Python example is roughly equivalent to:

code:
puts "Ruby says, \"hello, %s!\"" % name


The Python example has advantages though. If I had multiple values to insert, as in the following example, I don't have to remember the order of them when I list the trailing variables.

code:
print "%(messenger)s says, \"hello, %(recipient)s\"" % {"recipient": name, "messenger": "Python"}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Jun 26, 2004 12:46 am   Post subject: (No subject)

Python is distinctly more strict about syntax than Ruby, so the conversion of "Decsion, decision..." will be relatively straightforward.

In Ruby
code:
puts "Hello, Ruby!"
print "I'm "
name = gets.strip

if name == ""
   puts "Ruby says, \"fine, ignore me!\""
elsif name == "Calvin" or name == "Linus" or name == "Sid"
   puts "Ruby says, \"Haaaaa haaaaa!\""
else
   puts "Ruby says, \"Hello #{name}\""
end


In Python
code:
import sys

print "Hello, Python!"
sys.stdout.write("I'm ")
name = sys.stdin.readline()
name = name.strip()

if name is "":
   print "Python says, \"fine, ignore me!\""
elif name is "Calvin" or name is "Linux" or name is "Sid":
   print "Python says, \"Haaaaa haaaaa!\""
else:
   print "Python says, \"hello %(name)s\"" % {"name": name}


In Ruby
code:
puts "Hello, Ruby!"
print "I'm "
name = gets.strip

if name == ""
   statement = "fine, ignore me!"
elsif name == "Calvin" or name == "Linus" or name == "Sid"
   statement = "Haaaaa haaaaa!"
else
   statement = "Hello #{name}"
end

puts "Ruby says, \"#{statement}\"" unless statement.empty?


In Python
code:
import sys

print "Hello, Python!"
sys.stdout.write("I'm ")
name = sys.stdin.readline()
name = name.strip()

if name is "":
   statement = "fine, ignore me!"
elif name is "Calvin" or name is "Linux" or name is "Sid":
   statement = "Haaaaa haaaaa!"
else:
   statement = "hello %(name)s" % {"name": name}

if statement is not "":
   print "Python says, \"%(msg)s\" % {"msg": statement}


In Ruby
code:
puts "Hello, Ruby!"
print "I'm "
name = gets.strip

if name == ""
   statement = "fine, ignore me!"
elsif name =~ /Calvin|Linus|Sid/
   statement = "Haaaaa haaaaa!"
else
   statement = "Hello #{name}"
end

puts "Ruby says, \"#{statement}\"" unless statement.empty?


In Python
code:
import sys
import re

print "Hello, Python!"
sys.stdout.write("I'm ")
name = sys.stdin.readline()
name = name.strip()
bad_name_pattern = re.compile("Calvin|Linus|Sid")

if name is "":
   statement = "fine, ignore me!"
elif bad_name_pattern.match(name):
   statement = "Haaaaa haaaaa!"
else:
   statement = "hello %(name)s" % {"name": name}

if statement is not "":
   print "Python says, \"%(msg)s\" % {"msg": statement}


Compare and Contrast

Python foregoes equivalents to Ruby's case...when...else...end construct and the lack of an equivalent for "then" means that this can't be rolled up into a one-liner. As well, such statements in Python don't return a value. Neither for that matter does assignment (the = operator).

The postfix notation is missing too, along with the negative version of "if" ("unless"). "elsif", however, is further shortened to simply "elif".

Regular expressions are not a part of the Python language as they are in Ruby, but are implemented through a separate module. "re.compile" takes a pattern and creates a regular expression object which can be matched against a string variable.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: