Newbie question: Simple Ruby Date Question

Hi everyone

I am playing around with ruby. And as a Java developer I wrote this
method to get me the weekdays of a month:

···

########
  def weekdays(month)
    current = Date.new(month.year,month.month,01)
    result = Array.new
    while (current.month == month.month)
      if(current.wday != 0 && current.wday != 6)
        result.push(current)
      end
      current += 1
    end
    return result
  end
########

What would be a nice ruby way to write this?

Here is a slightly more Rubyish way to write this:

class Date
   def weekdays
      current = Date.new(year, month, 01)
      result =
      while current.month == month
         result << current if (1..5) === current.wday
         current += 1
      end
      result
   end
end

Now if you have a Date you can just call weekdays on it. I've taken out a
few unneeded parenthesis, but leaving them in wouldn't necessarily be an
unRuby way to write. Also the wday comparision now uses a Range and the
=== operator. This could also be written (1..5).member?(current.wday). In
addition I've decided to use << instead of push, and have also used the
syntax which puts the if at the end of the line (I'm not sure what the
official name is.) Lastly, there is no need for return if what you are
returning is the last line in the method.

Ryan Leavengood

mattscape wrote:

···

Hi everyone

I am playing around with ruby. And as a Java developer I wrote this
method to get me the weekdays of a month:
########
  def weekdays(month)
    current = Date.new(month.year,month.month,01)
    result = Array.new
    while (current.month == month.month)
      if(current.wday != 0 && current.wday != 6)
        result.push(current)
      end
      current += 1
    end
    return result
  end
########

What would be a nice ruby way to write this?

In Message-Id: <1114639618.103794.222300@o13g2000cwo.googlegroups.com>
"mattscape" <matthias.luebken@gmail.com> writes:

What would be a nice ruby way to write this?

Rather functional than rubyish though....

  (Date.new(month.year, month.month, 1)..Date.new(month.year, month.month, -1)).select {|d| (1..5).include?(d.wday)}

is sufficient?

Date has instance method succ, so can generate Range, then we can use
Enumerable#select for filtering elements in the Range.

···

--
kjana@dm4lab.to April 28, 2005
Translators, traitors.

Thanks !
I guess I like Ryans version better.
Mostly because I understand it :wink:

The version of Kazuhisa is not that hard to understand.
First:
(Date.new(month.year, month.month, 1)..Date.new(month.year, month.month, -1))
Is the range of days in the given month. (-1 is often used as index
for the last item, the same applies e.g. to Array and String).

Then he selects those elements from the range that have a weekday
between 1 and 5, that are all the days you wanted. The result is an
array of the weekdays of the month.

You will see that this reads a lot simpler than my english description
or your procedural code once you understood it.

Or break it in two lines then it is nearly english
days_of_the_month = (Date.new(month.year, month.month,
1)..Date.new(month.year, month.month, -1))
weekdays_of_month = days_of_the_month.select { | day | (1..5).include?day.wday }

best regards,

Brian

···

On 28/04/05, mattscape <matthias.luebken@gmail.com> wrote:

Thanks !
I guess I like Ryans version better.
Mostly because I understand it :wink:

--
http://ruby.brian-schroeder.de/

multilingual _non rails_ ruby based vocabulary trainer:
http://www.vocabulaire.org/ | http://www.gloser.org/ | http://www.vokabeln.net/