Newest Downloads

Mar.18

  [zenoss@zenoss2 ~]$ python Zenoss_Template_Manager.py -husage: Examples: python Zenoss_Templa...

From Perl To Python To Ruby..
Blog - Dynastys Blog
Written by Allen Sanabria   
Sunday, 08 March 2009 18:08

Well, I want to start this off, by saying that I am not leaving Python or Perl behind for Ruby, but that is the order in which I learned these 3 languages.  I learned Perl first and I used it strictly for about 3 years, then I learned Python and I have been using Python mainly since then and Perl when needed. Now since I am playing with Puppet, I figured I should go ahead and learn Ruby as well.

So far I am pretty impressed with Ruby as it reminds me of Python and Perl put together. Everything in Ruby is a Object just like it is in Python. Also you do not require a semicolon at the end of every line in Ruby like you do in Perl. You do not require a $ for a Scalar variable or a @ for an array or a % for a hash in Ruby, which you do require in Perl but not in Python. There are a lot more similarities then that, and I will post some of the commands that are similar and ones that are a bit different. Now this will not be a complete list of differences but enough so that you get the idea.

python	print "hello world" 			#This will print hello world but with a New Line at the End
ruby puts "hello world" #This will print hello world but with a New Line at the End
perl print "hello world\n"; #This will print hello world but with a New Line at the End

python x = 8 #x is equal to 8 which is an integer
ruby x = 8 #x is equal to 8 which is an integer
perl $x = 8; #x is equal to 8 which is an integer

python type(x) #x is what type.... Integer, String, Class... etc
ruby x.class #x is of what class.... FixNum, String, Class.. etc

python x, y, z = 1, 2, 3 #multiple assignments on one line
ruby x, y, z = 1, 2, 3 #multiple assignments on one line
perl ($x, $y, $z) = (1, 2, 3); #multiple assignments on one line

python se = "foil" #Assign a string to se
ruby se = "foil" #Assign a string to se
perl $se = "foil" #Assign a string to se

python se[0:4] #Access a slice of a string
ruby se[0..4] #Access a slice of a string


Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
Comments
Search RSS
dexxtreme   |Author |2009-03-09 05:13:31
One of these days I will have to actually learn Python. A lot of code on many of
our internal systems (namely, our primary customer database and support system)
is Python, and I'm one of very few people that still use Perl. One of
the first lessons I had to learn when modifying a Python script is that Python
is highly sensitive to whitespace. I ran into this issue when modifying an
"if" statement. In Perl, the amount of whitespace is irrelevant, but in
Python, the wrong amount of whitespace leads to syntax errors.
dynasty  - re:   |SAdministrator |2009-03-09 05:32:17
Actually Python is space sensitive but not as you think. Python uses spaces to
determine where a function or a loop or a class starts and ends.

Take a look at
a couple of my python scripts and you can tell from their.
dexxtreme   |Author |2009-03-10 13:44:37
Well, coming from Perl (where an entire program can be shoved into one line with
no linebreaks or spaces), it is still a bit of a culture shock. We normally
only use linebreaks and whitespace mainly for formatting and readability issues.
dynasty   |SAdministrator |2009-03-09 05:34:24
Also I'm thinking so I can learn Ruby quicker, I think I should convert most if
not all of my python scripts into Ruby.

And then maybe create some HowTo's on
Ruby and Python and compare them to each other.

I think that would help people
in learning both languages. What do you think?
chahn   |Administrator |2009-03-17 03:31:48
python type(x) #x is what type.... Integer, String, Class...
etc
ruby x.class #x is of what class.... FixNum, String, Class..
etc
perl ref $x #type of reference or class (SCALAR, ARRAY, HASH,
HTTP::Request) as string, empty string if not a
ref

python se[0:4] #Access a slice of a string
ruby se[0..4] #Access
a slice of a string
perl substr $se, 0, 4; #Access a slice of a
string.


Just sayin'.
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated on Sunday, 15 March 2009 17:02