That'd be the New Topic button then! This one.nik-w wrote:So instead of pestering you on LJ, I thought I'd check this place out, but I seem to be suffering from brain fade, as I can't work out how to start a new topic!:p
Was just going to say how I decided to do something productive with my week off work and take a look at this whole Ruby/Rails lark that people seem to rave about, but having looked at various tutorials online, I am more confused than ever!:p Considering I never entirely got the hang of C++ / OO stuff in general, it's not entirely surprising!:p I thought I'd start off by just rewriting a simple PHP/mysql script in ruby to get the hang of it, but it looks a lot harder than PHP!
Maybe you'd be better off learning a bit of Ruby on its own first. That's what I did since I actually wasn't interested in Rails to begin with. I taught myself Ruby from the online version of The Pickaxe, so-called because it has a picture of a pickaxe on the front! It has long been considered to be the Ruby bible. I bought a hard copy of the second edition a couple of months later but by that time, I already had a solid grasp of the language. I occasionally use it now to check on some of the finer points of the language. The second edition covers Ruby 1.8. The first edition covers 1.6 and the new third edition covers 1.9. No one uses 1.6 anymore but the book is still accurate for the most part so if you don't feel like shelling out any cash, it's worth a look. If The Pickaxe is the bible then Why’s (Poignant) Guide to Ruby would have to be the Life of Brian. It's totally off the wall but very highly acclaimed. I've not read it myself but if I was going to learn Ruby today, this is probably where I'd look. The author, why (who looks strangely like Jack Black), is a total legend. No one even knows his real name.
The thing about Ruby is that even though it's one of the most object oriented languages I know (even numbers like 5 are an object), you can bend it backwards to do all sorts of crazy things. This is in stark contrast to Java, which is also considered to be very object oriented but much more rigid. Here is an example of some of the silly things you can do.
Code: Select all
chewi@symphony % irb
irb(main):001:0> 1 + 1
=> 2
irb(main):002:0> class Fixnum
irb(main):003:1> alias_method :old_plus, :+
irb(main):004:1* def +(other)
irb(main):005:2> return 3 if self == 1 and other == 1
irb(main):006:2> old_plus(other)
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> 1 + 1
=> 3
irb(main):010:0> 1 + 2
=> 3
irb(main):011:0> 1 + 3
=> 4
One Ruby concept I found tricky to begin with is symbols. I didn't really understand what they were for. They're a bit like strings but different. If you have variables x, y and z that all hold the string "hello" then unless one of the variables points to another, each of those strings will occupy a separate area of memory. However, if you have variables x, y and z that all hold the symbol :hello then they will all point to the same area of memory. For this reason, it is not possible to do string operations on symbols, unless you convert them to a string first. So are they just for saving memory when string operations aren't needed? Sort of but I think there's more to it than that. Sometimes it is useful to be able to pass an argument to a method and say "If it's a string, do this, but if it's a symbol, do something else." I'm not doing a great job of explaining them here. The trouble is that a lot of the time, they're interchangeable! The difference is subtle but you'll come to realise what they're really for the more you work with Ruby code.
One thing Rails does all the time is use hashes for passing arguments around. This confused me at first so I'll explain it. If you're like me then you always forget the order of the arguments for various PHP functions and you end up digging through the manual to find out. Rails totally avoids that by giving the arguments names and you pass them through using a hash. For example..
Code: Select all
validates_numericality_of :price, { :only_integer => true, :maximum => 999, :minimum => 1 }
validates_numericality_of :price, :minimum => 1, :maximum => 999, :only_integer => true
validates_numericality_of('price', 1, 999,,,, true)
Hope this helps get you started.