static public Method

File.extname(p1)

File.extname(path)  string

Returns the extension (the portion of file name in path after the period).

File.extname("test.rb")         #=> ".rb"
File.extname("a/b/d/test.rb")   #=> ".rb"
File.extname("test")            #=> ""
File.extname(".profile")        #=> ""

Source Code

/*
*  call-seq:
*     File.extname(path) -> string
*  
*  Returns the extension (the portion of file name in <i>path</i>
*  after the period).
*     
*     File.extname("test.rb")         #=> ".rb"
*     File.extname("a/b/d/test.rb")   #=> ".rb"
*     File.extname("test")            #=> ""
*     File.extname(".profile")        #=> ""
*     
*/

static VALUE
rb_file_s_extname(klass, fname)
   VALUE klass, fname;
{
   char *name, *p, *e;
   VALUE extname;

   name = StringValueCStr(fname);
   p = strrdirsep(name);       /* get the last path component */
   if (!p)
       p = name;
   else
       p++;

    e = strrchr(p, '.');       /* get the last dot of the last component */
    if (!e || e == p || !e[1]) /* no dot, or the only dot is first or end? */
        return rb_str_new2("");
    extname = rb_str_new(e, chompdirsep(e) - e);       /* keep the dot, too! */
    OBJ_INFECT(extname, fname);
    return extname;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.