SitePoint Sponsor |
|
User Tag List
Results 1 to 21 of 21
Thread: Gotchas
-
Apr 10, 2006, 09:50 #1
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Gotchas
Ruby on Rails is a fabulous frame work. However, I have been surprised by a couple of gotchas:
Development log files can get very big very quickly
I didn't notice this until I copied a development framework to a production server and suddenly lost a lot of disk space. The development.log file was over a gig. Of course, I should have been more careful with what I copied over but I wasn't..
YAML files don't like tabs
When I first started working with Rails, I was using an editor that put tabs in when I indented content. No problem until I started editing database.yml and got lots of strange errors. It took me a while to track down the problem.
I now set up editors to only enter spaces.
Ruby isn't very clever with dates
I've found you have to be very cautious with dates. It doesn't help that Ruby has two data types (Time and Date) that do similar things, but are different. I try to use Datetime everywhere, but still get the odd surprise.
Rail's handling of months isn't too clever either. 3.months.ago doesn't actually take you back three months but rather 3 times 30 days. That's well documented, but can catch out the unwary (ok - I mean it caught me out). Also worth pointing out that what Rails actually does is subtract 30 times the number of seconds in a day, from the current Time to go back one "month".
Dividing an integer by an integer returns an integer
1/2 = 0
My more experienced programmings friend say "of course it does", but it caught me out. The solution:
1.0/2.0 = 0.5
Having said all that, otherwise developing using Ruby on Rails has been a joy. I love it.
Any other gotchas out there that I've yet to find?Last edited by ReggieB; Apr 18, 2006 at 08:58.
-
Apr 10, 2006, 10:16 #2
I got caught out by scope/access when I first got into Ruby. I didn't think that this:
Code:class Whatever def method1 end protected def method2 end end
Code:class Whatever protected def method2 end def method1 end end
-
Apr 10, 2006, 14:27 #3
- Join Date
- May 2002
- Location
- Berkeley
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Don't ever name an instance variable in a controller @template
I suppose some people would say "Duh," but I had created a controller for generating html templates and at the time it seemed logical to name an instance variable @template. Anyway, doing this totally blows up the app.
-
Apr 11, 2006, 05:21 #4
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
My favorite gotcha is that "0" is "true". First time it costed me two hours to find out why "if(!count)" doesn't work...
Originally Posted by ReggieB
Code:class Fixnum alias idiv / def /(other) self.to_f / other end end print 1/2
-
Apr 17, 2006, 02:07 #5
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This is not a real gotcha, but it is good to know.
Don't stay away from the features other languages don't have, just because you don't know them.
This code, for example:
Code:time = '2006-04-17 10:57' year = time[0..3] month = time[5..6] day = time[8..9] hour = time[11..12] min = time[14..15] Time.local(year, month, day, hour, min) => Mon Apr 17 10:57:00 West-Europe(GMT) 2006
Code:time = '2006-04-17 10:57' Time.local(*[0..3, 5..6, 8..9, 11..12, 14..15].map{|r| time[r]}) => Mon Apr 17 10:57:00 West-Europe(GMT) 2006
So use this:
Code:name = if only_first_name? first_name else [first_name, last_name].join(' ') end
Code:if only_first_name? name = first_name else name = [first_name, last_name].join(' ') end
And remember that (0..3).to_a => [0, 1, 2, 3] and (0...3).to_a => [0, 1, 2].
Everything is true, *except* false and nil. So [], 0, {} are all true. Use obj.blank? to convert it to bools: [].blank? => true "".blank? => true, etc.
-
Apr 17, 2006, 08:38 #6
- Join Date
- Jul 2004
- Location
- Oklahoma
- Posts
- 119
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by ReggieB
-
Apr 17, 2006, 18:47 #7
- Join Date
- Jun 2004
- Location
- California
- Posts
- 440
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Sgarissta
-
Apr 18, 2006, 04:11 #8
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Fenrir2
Code:Time.local(*time.match(/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+)/)[1..-1])
-
Apr 18, 2006, 05:21 #9
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Sgarissta
Originally Posted by ReggieB
-
Apr 18, 2006, 06:58 #10
- Join Date
- Jul 2004
- Location
- Oklahoma
- Posts
- 119
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by ReggieB
-
Apr 18, 2006, 08:57 #11
- Join Date
- Feb 2006
- Location
- Worcs. UK
- Posts
- 404
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Sgarissta
-
Apr 18, 2006, 09:03 #12
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:Time.local(*[0..3,5..6,8..9,11..12,14..15].map{|r|time[r]}) Time.local(*time.match(/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+)/)[1..-1])
1aa3?21&78`87^12
Would be recognized as a valid thing by mine, but yours will see that it isn't
But all this comes for the cost of 6 extra bytes ;-).
-
Apr 19, 2006, 14:38 #13
Originally Posted by stereofrog
quick example
Code:if rating_percentage puts rating_percentage.to_s + '%' else puts 'no rating yet' end
Are there any "cheat sheets" listing all these naming conventions?Ohai!
-
Apr 19, 2006, 15:33 #14
Originally Posted by Majglow
Models: singular
Controllers: plural
Any others?
-
Apr 19, 2006, 15:37 #15
Wait... controllers: plural? what do you mean?
Ohai!
-
Apr 19, 2006, 15:39 #16
Originally Posted by Majglow
Code:ruby script/generate controller pages
-
Apr 20, 2006, 10:50 #17
Couple of cheat sheets:
http://www.ilovejackdaniels.com/ruby...s-cheat-sheet/
and this one which I think does cover the naming conventions:
http://www.blainekendall.com/index.p...ilscheatsheet/Erh
-
Apr 20, 2006, 11:05 #18
- Join Date
- Nov 2004
- Location
- Romania
- Posts
- 848
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The block syntax with do ... end is not the same as the one that uses braces.
Thus ...
Code:v = [1,2,3,4] # correct x = v.find do |x| x % 3 == 0 end puts x # correct puts v.find { |x| x % 3 == 0 } # syntax error puts v.find do |x| x % 3 == 0 end # syntax error puts(v.find do |x| x % 3 == 0 end)
-
Apr 20, 2006, 13:08 #19
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This gotcha also works the other way around:
Code:irb(main):001:0> def a(b) irb(main):002:1> yield b irb(main):003:1> end => nil irb(main):004:0> a 3 do |v| puts v; end 3 => nil irb(main):005:0> a 3 {|v| puts v} SyntaxError: compile error (irb):5: syntax error a 3 {|v| puts v} ^ from (irb):5 irb(main):006:0> a(3){|v| puts v} 3 => nil
-
Apr 20, 2006, 18:25 #20
- Join Date
- Jun 2004
- Location
- California
- Posts
- 440
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Not really a gotcha as it is working as expected but this has bitten me in the butt a few times for not remembering: redirect_to and render do not end the action there. If you call a redirect_to or render later, after already calling one of either or both, you will get a double render error. Rails will not override the redirect to the new nor render the new. A way to get past this, if you wish the action to end at that redirect or render is to return that:
Code:def bla ... return redirect_to :action => :index if something.problem? ... render :action => :bla end
-
Apr 21, 2006, 01:10 #21
- Join Date
- Apr 2004
- Location
- germany
- Posts
- 4,324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Perhaps everyone's seen this before, but still
Things That Newcomers to Ruby Should Know -- nice list of gotchas
How Ruby Sucks by Matz.
Bookmarks