# \<\< : can some body check this code?

**URL:** <https://www.ruby-forum.com/t/can-some-body-check-this-code/131878>\
**Category:** Ruby\
**Created:** [March 14, 2008, 2:33am UTC](https://www.ruby-forum.com/t/can-some-body-check-this-code/131878 "2008-03-14T02:33:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 14, 2008, 2:33am UTC](https://www.ruby-forum.com/t/can-some-body-check-this-code/131878/1 "2008-03-14T02:33:24Z")

</div>

why is it not working properly( weekly calendar)…  
require “Date”  
def mon\_days(month,year=Date.today.year)  
mday = [nil,31,28,31,30,31,30,31,31,30,31,30,31]  
mday[2] = 29 if Date.leap? year  
mday[month]  
end

def cal\_week(month,year=Date.today.year)  
days = 1…mon\_days(month,year)  
weeks = []  
week = []  
wday = 1  
for i in days  
wday = Date.new(year,month,i).wday  
week[wday] = i  
if wday == 6  
weeks \<\< week  
p week,weeks  
week.clear

```
end

```

end  
weeks \<\< week if week.length != 0  
weeks  
end

p cal\_week(4)

I think it is the problem of week still remembering the values…  
THnks  
CHinna

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 14, 2008, 2:37am UTC](https://www.ruby-forum.com/t/can-some-body-check-this-code/131878/2 "2008-03-14T02:37:31Z")

</div>

I think I found an answer not sure if it is normal way to do things…  
weeks \<\< week.clone instead of week…  
Looking forward for comments…  
THnks

> ```
> weeks << week
> p week,weeks
> week.clear
> 
> end
> 
> ```
> 
> end  
> weeks \<\< week if week.length != 0  
> weeks  
> end
> 
> p cal\_week(4)
> 
> I think it is the problem of week still remembering the values…  
> THnks  
> CHinna

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 14, 2008, 6:30am UTC](https://www.ruby-forum.com/t/can-some-body-check-this-code/131878/3 "2008-03-14T06:30:17Z")

</div>

On Thu, Mar 13, 2008 at 8:40 PM, Chinna K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > week.clear
> > 
> > ```
> > 
> > THnks  
> > CHinna

Yes, you are using the same Array object over and over with  
week.clear. I tend to just do week = [] instead of clone.

Todd

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 14, 2008, 6:35am UTC](https://www.ruby-forum.com/t/can-some-body-check-this-code/131878/4 "2008-03-14T06:35:34Z")

</div>

On Fri, Mar 14, 2008 at 12:29 AM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > p cal\_week(4)
> > > 
> > > I think it is the problem of week still remembering the values…  
> > > THnks  
> > > CHinna
> 
> Yes, you are using the same Array object over and over with  
> week.clear. I tend to just do week = [] instead of clone.
> 
> Todd

Sorry, didn’t post example. Here it is using your code…

require “Date”  
def mon\_days(month,year=Date.today.year)  
mday = [nil,31,28,31,30,31,30,31,31,30,31,30,31]  
mday[2] = 29 if Date.leap? year  
mday[month]  
end

def cal\_week(month,year=Date.today.year)  
days = 1…mon\_days(month,year)  
weeks = []  
week = []  
wday = 1  
for i in days  
wday = Date.new(year,month,i).wday  
week[wday] = i  
if wday == 6  
weeks \<\< week  
week = []  
end  
end  
weeks \<\< week if week.length != 0  
weeks  
end

p cal\_week(4)

Todd

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 14, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/can-some-body-check-this-code/131878/5 "2008-03-14T16:59:13Z")

</div>

Thanks Todd, Rolando . I got the point (week[])…  
I appreciate your help and time.  
THnks

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 14, 2008, 12:46pm UTC](https://www.ruby-forum.com/t/can-some-body-check-this-code/131878/6 "2008-03-14T12:46:16Z")

</div>

On Mar 13, 2008, at 9:33 PM, Chinna K. wrote:

> weeks = []  
> end  
> end  
> weeks \<\< week if week.length != 0  
> weeks  
> end
> 
> p cal\_week(4)
> 
> I think it is the problem of week still remembering the values…

the problem is that you’re using the same week array. Here’s another  
solution for the same problem:

def cal\_week(month, year = Date.today.year)  
fotm = Date.new(year,month,1) # first of the month  
lotm = (fotm \>\> 1) - 1 # last of the month  
weeks = []  
week = []  
fotm.upto(lotm) do |d|  
week[d.wday] = d.day  
if d.wday == 6  
weeks \<\< week  
week = []  
end  
end  
weeks \<\< week unless week.empty?  
end

p cal\_week(4)

> ## THnks CHinna
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

regards,
