Formatting dates and counting words

Hi,

Just got started on making a real attempt in building a Ruby app, and got
some basic questions:

How do I format a date? E.g. something like PHP’s date() function where it
can show a date as a string in various formats:

http://www.php.net/manual/en/function.date.php

Secondly, given a string, how do I get the first few words from it? e.g. If
a string has 50 words, I want the first 10 words return.

Thanks

Robo

Hi,

Just got started on making a real attempt in building a Ruby app, and got
some basic questions:

How do I format a date? E.g. something like PHP’s date() function where it
can show a date as a string in various formats:

I believe Time#strftime() is what you need

http://www.php.net/manual/en/function.date.php

Secondly, given a string, how do I get the first few words from it? e.g. If
a string has 50 words, I want the first 10 words return.

please define what “word” means.
usually this could work:

s=“this is a string of \n7 words”
=> “this is \ta string of \n7 words”
ary=s.split(/\s+/)
=> [“this”, “is”, “a”, “string”, “of”, “7”, “words”]
ary[0…2]
=> [“this”, “is”, “a”]

Or you could use \W+ or [a-z]+ or…

···

il Sun, 28 Mar 2004 03:07:44 +1200, “Robo” robo@mars.com ha scritto::

I believe Time#strftime() is what you need

Is there doc for that function? I looked it up in ruby-doc.org, it has at
least one line line comment on every method in the Date class except for the
strftime() which is completely blank.

So, how do I find info on characters it recognise and how it intepret each
character? PHP has a table in the link in my first post, I looked at Ruby’s
source for strftime(), and the formats are not quite the same.

Robo

The strftime function probably isn’t documented because it’s not a Ruby thing.
It’s a standard C library function that Ruby just provides an interface to.
That’s not an excuse, of course, but there are more important and Ruby-specific
things whose documentation is a higher priority.

The standard manual page for strftime from section 3 of the Unix Programmer’s
Manual is reproduced in various places around the web. The first google
hit for “strftime man page” was this:

http://www.cgi101.com/class/ch16/envman.cgi?strftime

Robo wrote:

I believe Time#strftime() is what you need

Is there doc for that function? I looked it up in ruby-doc.org, it has at
least one line line comment on every method in the Date class except for the
strftime() which is completely blank.

So, how do I find info on characters it recognise and how it intepret each
character? PHP has a table in the link in my first post, I looked at Ruby’s
source for strftime(), and the formats are not quite the same.

The rdoc for this in ruby 1.9 is, I believe, accurate for 1.8 as well

http://www.ruby-doc.org/docs/rdoc/1.9/
(It’s a frameset, so i don’t have a link directly to the Time lib, but
you can browser the index in the top left.)

James