From: 7stud -- Date: 2009-10-06T14:42:12+09:00 Subject: Re: Ruby for the wrong reason flebber wrote: > Hi > > A few months ago, I started learning Python - my first language for a > project I want to do (personal) which will involve several facets > including MySql/postgres database and gui frontend I need to create on > windows xp. So I started this in python and I use Eric 4 ide. > > At the time I had no knowledge and tossed up between Ruby and Python, > I chose python because I thought it had been around longer so may be > more mature. > > So why Ruby now, well honestly my 3rd daughter has been born two days > ago Ruby Jean, why would I program Python when my daughter is Ruby? > > My question would Eric4 still be a good choice for Ide in Ruby, code > completion, highlighting(syntax) and debugging would be required in > IDE. > > Secondly I found capturing the date input from a console in python to > be a little long winded, below is the script I wrote in python would > this be significantly easier in Ruby. > > Which version of Ruby should I use 1.8.6, 1.9.1 or a preview version > of 1.9.2? > > My python script to capture date input and check for errors. > > import datetime as dt > def ObtainDate(): > isValid=False > while not isValid: > userIn = raw_input("Type Date dd/mm/yy: ") > try: # strptime throws an exception if the input doesn't match > the pattern > d1 = dt.datetime.strptime(userIn, "%d/%m/%y") > isValid=True > print d1, type(d1) # 2003-07-15 00:00:00 'datetime.datetime'> > "convert datetime object to a 10 character string" > d2 = str(d1)[:10] > print d2, type(d2) # 2003-07-15 > except: > print "Try again! dd/mm/yy\n" > Fname = "data.dat" > newFname = d2 + Fname > return newFname > print ObtainDate() This is a good example for you to investigate on your own. It will introduce you to ruby's documentation(or lack thereof). Here's how I would do it in python: def get_new_fname(fname): while True: try: s = raw_input("Enter a date (dd/mm/yy): ") in_format = "%d/%m/%y" dt_obj = dt.datetime.strptime(s, in_format) break except ValueError: print "Wrong format! Try again." out_format = "%Y-%m-%d" prefix = dt_obj.strftime(out_format) new_fname = "%s%s" % (prefix, fname) return new_fname name = "data.dat" new_name = get_new_fname(name) print new_name --output:-- 2009-10-05data.dat -- Posted via http://www.ruby-forum.com/.