SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function questions (Coming from coldfusion)

    As one of my first 'things' to build with ruby (to help me learn the language) I wanted to make a calendar. Now I'm a designer that is fairly comfortable with coldfusion, so the process for a webapp is completely foreign to me.

    My question is whether or not some functions exist or something comparative to them.

    DayAsString() - this takes an integer, 1-7 and outputs Sunday - Saturday
    MonthAsString() - this takes an integer, 1-12 and outputs January - December
    DaysInMonth() - Takes an integer, 1-12 and returns total number of days in the specified month

    Are there any functions in this vein?
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In Ruby you'd channel your time needs through a Time object.

    Code Ruby:
    time = Time.now
    time.strftime("%A")
     
    #go crazy if you want
    def time.day_of_week
      self.strftime("%A")
    end
     
    #less crazy
    def time_to_day_of_week(time_obj)
      time_obj.strftime("%A")
    end
     
    time.day_of_week

    There's a full chart of these %keys.
    http://ruby-doc.org/core/classes/Time.html#M000297
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about the days in month, is there a simple way to get the days in a specified month?
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  4. #4
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    indirectly:

    Code:
    Date.civil(year,month,-1).day
    It takes (year,month,day)... here, -1 signifying "go to the last day of the month", then give me the number of day I'm on..

    via: http://snippets.scottwalter.com/posts/show/211
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  5. #5
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like that snippets site, thanks for mentioning that... however, when I try to run that code
    Code:
    year = Time.now.year
    month = Time.now.month
    total_days = Date.civil(year,month,-1).date
    puts total_days
    I'm getting an "uninitialized constant Date (NameError)" error. Any idea why?

    Sorry, Date is a rails function and not ruby. I don't like how the http://corelib.rubyonrails.org/ page has ruby 1.8.4 for the page title.
    Last edited by downtroden; Sep 6, 2007 at 07:03. Reason: i'm stupid
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Ruby:
    require 'date'
     
    def day_as_string(day)
        arr = Date::DAYNAMES   #Date is defined in the standard library file date.rb
        return arr[day-1]
    end
     
    def month_as_string(month)
        arr = Date::MONTHNAMES
        return arr[month]
    end
     
    def days_in_month(month, year=2007)
        return Date.new(year, month, -1).day 
    end
     
    #output the days of the week corresponding to the numbers 1-7:
    (1..7).each {|num| print day_as_string(num), " "}
    puts
     
    #output the month names and the number of days in each month
    #corresponding to the numbers 1-12:
    (1..12).each do |num| 
        puts "#{month_as_string(num)} (#{days_in_month(num)})"
    end
     
    --output:--
    Sunday Monday Tuesday Wednesday Thursday Friday Saturday 
    January (31)
    February (28)
    March (31)
    April (30)
    May (31)
    June (30)
    July (31)
    August (31)
    September (30)
    October (31)
    November (30)
    December (31)
    Last edited by 7stud; Sep 7, 2007 at 13:28.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •