Ruby for the wrong reason

F

flebber

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 <type
'datetime.datetime'>
"convert datetime object to a 10 character string"
d2 = str(d1)[:10]
print d2, type(d2) # 2003-07-15 <type 'str'>
except:
print "Try again! dd/mm/yy\n"
Fname = "data.dat"
newFname = d2 + Fname
return newFname
print ObtainDate()
 
R

Rajinder Yadav

flebber said:
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.

I went through this same process, even tried learning Perl which I disliked,
tasted like I chalk in my mouth. Being a C++ developer, Ruby was my natural
choice (front runner). Python lost me because I thought it was just stupid to
only have indentations to declare code blocks. Other Python users told me I
would overcome this. Someone did a good job of trying to sell me Python + Django
as the way to go. So even though Ruby was more fun and felt natural to me, I
still struggled with what to go with.

In the end I didn't pick Ruby, Ruby picked me. I just when with what felt good
to me and then the rest didn't matter what was better.

There are never no wrong reasons, it's better to explore and then go with the
flow for what feels good to you, not someone else.

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.

Someone was capturing the various IDE in a spreadsheet, I don't have that email
around...possibly they will reply with the link soon =)

But I suggest you take a look at Aptana, it's got everything you need and it
will also allow you to create Rails applications.

http://aptana.com

Enjoy and save the python for the wife ;)
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 <type
'datetime.datetime'>
"convert datetime object to a 10 character string"
d2 = str(d1)[:10]
print d2, type(d2) # 2003-07-15 <type 'str'>
except:
print "Try again! dd/mm/yy\n"
Fname = "data.dat"
newFname = d2 + Fname
return newFname
print ObtainDate()


--
Kind Regards,
Rajinder Yadav

http://DevMentor.org
Do Good ~ Share Freely
 
7

7stud --

flebber said:
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 <type
'datetime.datetime'>
"convert datetime object to a 10 character string"
d2 = str(d1)[:10]
print d2, type(d2) # 2003-07-15 <type 'str'>
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
 
7

7stud --

flebber said:
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.

No. I think in ruby the code would be almost exactly the same--except
it's significantly harder to figure out in ruby because there is no
documentation about the format specifiers you can use with strftime()
and strptime(). Amazing, huh?
Which version of Ruby should I use 1.8.6, 1.9.1 or a preview version
of 1.9.2?

ruby and python are in the same state of evolution right now--both are
in the midst of a major version change. In both cases, the newer
versions won't have as many libraries available to them because most
libraries have not yet been rewritten to accommodate the latest version.
So the choice is: go with ruby 1.8.6 and have more libraries available
to you, or go with ruby 1.9.x to learn the latest greatest language
changes. I think most people probably have 1.8.6 and 1.9.x installed
side by side(in python it would be 2.6.x and 3.x). They probably use
1.8.6 for their serious programs, and then they play around with 1.9.x
to learn the new stuff.
 
A

Adam Bilbrough

flebber said:
So If I use version 1.8.6 and later want to update my project to the
1.9 series would this be a hard conversion?

For Ruby, no. Tt has no specific indentations to follow (within reason)
and the only things that have changed as far as I know (been a while
since I read the release docs) 1.9.1 adds some new functionality to Ruby
but doesn't change the syntax (like Python 2 - 3 did).
With my program I was working on, I was using 1.9.1 (on my old linux
distro) and also running it on my Mac (1.8.7), so as far as the scope of
my program was concerned, the versions were universally capable of
running on either system with zero changes.

Hope this helps!
 
D

David A. Black

Hi --

For Ruby, no. Tt has no specific indentations to follow (within reason)
and the only things that have changed as far as I know (been a while
since I read the release docs) 1.9.1 adds some new functionality to Ruby
but doesn't change the syntax (like Python 2 - 3 did).
With my program I was working on, I was using 1.9.1 (on my old linux
distro) and also running it on my Mac (1.8.7), so as far as the scope of
my program was concerned, the versions were universally capable of
running on either system with zero changes.

The syntax has actually changed in certain respects. See:

http://dablog.rubypal.com/2009/1/14/10-things-to-be-aware-of-in-moving-to-ruby-1-9

and the sequel post (there's a link).


David
 
D

David A. Black

Hi --

No. I think in ruby the code would be almost exactly the same--except
it's significantly harder to figure out in ruby because there is no
documentation about the format specifiers you can use with strftime()
and strptime(). Amazing, huh?

I usually go straight to the system library documentation for this
stuff, but ri Time#strftime will help you too.


David
 
J

James Edward Gray II

No. I think in ruby the code would be almost exactly the same--except
it's significantly harder to figure out in ruby because there is no
documentation about the format specifiers you can use with strftime()
and strptime(). Amazing, huh?

Amazingly wrong, yeah:

---------------------------------------------------------- Time#strftime
time.strftime( string ) => string
------------------------------------------------------------------------
Formats _time_ according to the directives in the given format
string. Any text not listed as a directive will be passed through
to the output string.

Format meaning:

%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character

t = Time.now
t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=> "at 08:56AM"

James Edward Gray II
 
D

David Masover

Which version of Ruby should I use 1.8.6, 1.9.1 or a preview version
of 1.9.2?

If it's a brand-new project, use 1.9.1.

Unless you do something strange, it should be portable backwards to 1.8.7, and
with a few (very small) patches, to 1.8.6.

As others say, not all libraries will be available on 1.9.1, but the vast,
VAST majority are. If you have a choice between two libraries, one which works
on 1.9.1, and one which doesn't, that's a strong hint about which is more
actively maintained.

And all other things equal, 1.9.1 is twice as fast as 1.8.6, so there isn't a
good reason not to use it.

On an existing project, that depends entirely on whether the project has been
ported to 1.9 yet.

The preview version, you could install it side by side and test, to make sure
everything still works. But you generally want to use a stable version -- just
not a stable-as-in-Debian-stable version ;)
 
G

Glen Holcomb

On Oct 6, 2009, at 1:16 AM, 7stud -- wrote:

No. I think in ruby the code would be almost exactly the same--except

Amazingly wrong, yeah:

---------------------------------------------------------- Time#strftime
time.strftime( string ) =3D> string
------------------------------------------------------------------------
Formats _time_ according to the directives in the given format
string. Any text not listed as a directive will be passed through
to the output string.

Format meaning:

%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character

t =3D Time.now
t.strftime("Printed on %m/%d/%Y") #=3D> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=3D> "at 08:56AM"

James Edward Gray II
If anyone is curious where the information James provided can be found:

http://lmgtfy.com/?q=3DRuby+strftime&l=3D1

--=20
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can=92t hear a word you=92re saying."

-Greg Graffin (Bad Religion)
 
Z

Zachary Carter

And to think, this whole dilemma could have been avoided if you named
her Python.
 
S

Seebs

And all other things equal, 1.9.1 is twice as fast as 1.8.6, so there isn't a
good reason not to use it.

As of sometime recently, there were reports of some Rails stuff not working
on it, which might be a good reason.

-s
 
7

7stud --

James said:
Amazingly wrong, yeah:

Not at all:

1) $ri Date::strptime

--------------------------------------------------------- Date::strptime
Date::strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
------------------------------------------------------------------------
Create a new Date object by parsing from a String according to a
specified format.

+str+ is a String holding a date representation. +fmt+ is the
format that the date is in. See date/format.rb for details on
supported formats.

The default +str+ is '-4712-01-01', and the default +fmt+ is '%F',
which means Year-Month-Day_of_Month. This gives Julian Day Number
day 0.

+sg+ specifies the Day of Calendar Reform.

An ArgumentError will be raised if +str+ cannot be parsed.
-----------------------------

Good luck finding and then digging through the **source code** of
format.rb to locate anything on the format specifiers.


2) $ri Date#strftime

---------------------------------------------------------- Date#strftime
strftime(fmt='%F')
------------------------------------------------------------------------
(no description...)



And from the official Standard Library Documentation for 'date':

1)
strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
----------------------------------------

Create a new Date object by parsing from a String according to a
specified format.

str is a String holding a date representation. fmt is the format that
the date is in. See date/format.rb for details on supported formats.

The default str is ’-4712-01-01’, and the default fmt is ’%F’, which
means Year-Month-Day_of_Month. This gives Julian Day Number day 0.

sg specifies the Day of Calendar Reform.

An ArgumentError will be raised if str cannot be parsed.
=================


2)
strftime(fmt='%F')
 
G

Glen Holcomb

James said:
Amazingly wrong, yeah:

Not at all:

1) $ri Date::strptime

--------------------------------------------------------- Date::strptime
Date::strptime(str=3D'-4712-01-01', fmt=3D'%F', sg=3DITALY)
------------------------------------------------------------------------
Create a new Date object by parsing from a String according to a
specified format.

+str+ is a String holding a date representation. +fmt+ is the
format that the date is in. See date/format.rb for details on
supported formats.

The default +str+ is '-4712-01-01', and the default +fmt+ is '%F',
which means Year-Month-Day_of_Month. This gives Julian Day Number
day 0.

+sg+ specifies the Day of Calendar Reform.

An ArgumentError will be raised if +str+ cannot be parsed.
-----------------------------

Good luck finding and then digging through the **source code** of
format.rb to locate anything on the format specifiers.


2) $ri Date#strftime

---------------------------------------------------------- Date#strftime
strftime(fmt=3D'%F')
------------------------------------------------------------------------
(no description...)



And from the official Standard Library Documentation for 'date':

1)
strptime(str=3D'-4712-01-01', fmt=3D'%F', sg=3DITALY)
----------------------------------------

Create a new Date object by parsing from a String according to a
specified format.

str is a String holding a date representation. fmt is the format that
the date is in. See date/format.rb for details on supported formats.

The default str is =92-4712-01-01=92, and the default fmt is =92%F=92, wh= ich
means Year-Month-Day_of_Month. This gives Julian Day Number day 0.

sg specifies the Day of Calendar Reform.

An ArgumentError will be raised if str cannot be parsed.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D


2)
strftime(fmt=3D'%F')
--------------
[Source]

That last one is a real doozy--and quite typical of ruby Standard
Library documentation.
With the above approach one might benefit by starting with:

ri strftime

Then looking at "all" the strftime methods.

--=20
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can=92t hear a word you=92re saying."

-Greg Graffin (Bad Religion)
 
J

James Edward Gray II

That last one is a real doozy--and quite typical of ruby Standard
Library documentation.

You've been shown the format option from the documentation. I'm not
sure how much more proof you require.

I agree that they could be copied to the Date methods and any specific
format options for those methods could be added. But to say there's
no documentation was just plain wrong.

You've obviously gone into the source to find them once, to bad you
didn't create a documentation patch while you were there.

James Edward Gray II
 
M

Michal Suchanek

2009/10/6 7stud -- said:
James said:
Amazingly wrong, yeah:

Not at all:

1) $ri Date::strptime

--------------------------------------------------------- Date::strptime
=C2=A0 =C2=A0 Date::strptime(str=3D'-4712-01-01', fmt=3D'%F', sg=3DITALY)
------------------------------------------------------------------------
=C2=A0 =C2=A0 Create a new Date object by parsing from a String according= to a
=C2=A0 =C2=A0 specified format.

=C2=A0 =C2=A0 +str+ is a String holding a date representation. +fmt+ is t= he
=C2=A0 =C2=A0 format that the date is in. See date/format.rb for details = on
=C2=A0 =C2=A0 supported formats.

=C2=A0 =C2=A0 The default +str+ is '-4712-01-01', and the default +fmt+ i= s '%F',
=C2=A0 =C2=A0 which means Year-Month-Day_of_Month. This gives Julian Day = Number
=C2=A0 =C2=A0 day 0.

=C2=A0 =C2=A0 +sg+ specifies the Day of Calendar Reform.

=C2=A0 =C2=A0 An ArgumentError will be raised if +str+ cannot be parsed.
-----------------------------

Good luck finding and then digging through the **source code** of
format.rb to locate anything on the format specifiers.


2) $ri Date#strftime

---------------------------------------------------------- Date#strftime
=C2=A0 =C2=A0 strftime(fmt=3D'%F')
------------------------------------------------------------------------
=C2=A0 =C2=A0 (no description...)



And from the official Standard Library Documentation for 'date':

1)
strptime(str=3D'-4712-01-01', fmt=3D'%F', sg=3DITALY)
----------------------------------------

Create a new Date object by parsing from a String according to a
specified format.

str is a String holding a date representation. fmt is the format that
the date is in. See date/format.rb for details on supported formats.

The default str is =E2=80=99-4712-01-01=E2=80=99, and the default fmt is = =E2=80=99%F=E2=80=99, which
means Year-Month-Day_of_Month. This gives Julian Day Number day 0.

sg specifies the Day of Calendar Reform.

An ArgumentError will be raised if str cannot be parsed.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D


2)
strftime(fmt=3D'%F')
--------------
[Source]

That last one is a real doozy--and quite typical of ruby Standard
Library documentation.

In this case I use the strftime(3) man page but windows users are at a
disadvantage here.

The man pages can be found on the web, though.

And while there are things that could use better documentation there
is no need to replicate documentation of ancient function which is
standardized in POSIX and wherever else.

Thanks

Michal
 
7

7stud --

James said:
You've been shown the format option from the documentation. I'm not
sure how much more proof you require.

I already knew the format specifiers were described in the Time class.
The question is whether someone new to ruby could figure out how to
format Date objects. I think not. The reason: ruby has poor
documentation.
You've obviously gone into the source to find them once, to bad you
didn't create a documentation patch while you were there.

James Edward Gray II

I have suggested a better solution in the past, and I'll suggest it
again: open up the ruby docs to user comments like php does. If the
ruby community wants to continue with the worst docs I've ever seen,
then so be it.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top