31 Jul, 2011, Kline wrote in the 1st comment:
Votes: 0
So I'm pretty new to Ruby. As in this is the second functioning script I've written on my own; the first being a policy daemon for Adobe Flash sockets.

I really hate having to think of meals to cook every night of the week. It's like one of my absolute least favorite things to do. So, bless you Ruby, for you have given me a random weekly menu in only 12 lines of code. I dump text files named as recipes into a folder that contain my ingredient list and cooking directions. A PHP shell exec (all I know, sorry, I'm a PHP idiot too!) lets me run it via my browser to my LAN server. F5 and a whole 'nother week is presented :)

Any other cool little niche projects to make home life easier?

Dir.chdir("/home/matt/food")
recipe_list = Dir.glob("*.txt")
recipe_list.collect! { |i| i.chomp(".txt") }

selected = recipe_list.sample(7)
selected.each { |i|
puts i
File.open("#{i}.txt").each_line { |i|
puts i
}
puts ""
}

Sample output:


Just need to add some more meals to my folder and finish writing up the ones I did add :) Simple formatting gives me a 2-3pg long list to print, shop, then cook directly from each week now!
03 Aug, 2011, Runter wrote in the 2nd comment:
Votes: 0
Looks good.

I don't have any real complaints, but in the name of constructive criticism..

I think it could be simplified considerably.
Dir.chdir("/home/matt/food")
Dir.glob("*.txt")
.collect { |f| f.chomp(".txt") }
.sample(7)
.each { |file| puts [file, File.open("#{file}.txt").read(), ""] }


Also, I would suggest replacing { } with do .. end for multi-line blocks in the future. It's fairly widely accepted in the ruby community that the {} is for one liners.

# block notation for 1 line
3.times {|i| puts i}


vs

# block notation for multiple lines
100.times do |i|
if (i % 3==1) && (i % 2==1)
puts i
end
end



Glad you're finding every day uses for a high level language. I've got similar food script I use myself. :p
03 Aug, 2011, Kline wrote in the 3rd comment:
Votes: 0
Hey thanks for the input. I didn't realize I could stack all that on the Dir.glob(). I think I tried to stack a few after the .collect() but wasn't able to.
04 Aug, 2011, Runter wrote in the 4th comment:
Votes: 0
That was probably because of collect!.

The difference in the ! (bangb methods (in general) is they do a 'dangerous' operation when compared to the non-bang version.
A lot of times that just means it changes the immediate object rather than returning a duplicate which is changed.
So if it's not returning a duplicate, there's no way to chain method calls.
# prints the values 2, 4, 6
puts [1,2,3].collect {|i| i * 2}

vs
#prints the values 2, 4, 6
arr = [1,2,3]
arr.collect!{|i| i * 2}
puts arr


In full disclosure, it is more machine efficient to use the bang methods in general. But, in general, it won't matter.



In full disclosure, it is more machine efficient to use the bang methods in general. But, in general, it won't matter.

[
04 Aug, 2011, Kline wrote in the 5th comment:
Votes: 0
Ahh ok, thanks. Yeah, I intentionally wanted to modify the existing object and not have to place a duplicate one in a variable but I didn't realize it would stop me from chaining things together by doing so.
04 Aug, 2011, Runter wrote in the 6th comment:
Votes: 0
Kline said:
Ahh ok, thanks. Yeah, I intentionally wanted to modify the existing object and not have to place a duplicate one in a variable but I didn't realize it would stop me from chaining things together by doing so.


Actually, I was wrong. It should return self on the bang method collect. So that should be chainable still. Hrm…
0.0/6