Ruby on Rails

The hub of Aura activity, the cortex that binds the forum together.
User avatar
Chewi
Anti-Hero
Posts: 3521
Joined: Sat Jun 12, 2004 3:51 pm
Location: Edinburgh, Scotland

Post by Chewi » Mon Sep 08, 2008 2:16 pm

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!
That'd be the New Topic button then! This one. :P
Image

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
This is obviously an extreme example but rather than avoid these silly abuses of the language, Ruby coders actually embrace them to make their code easier to use. This is great for developers who use their code but if you ever want to try and find out how that code works, it can be quite a challenge! Rails is no exception to this. I'm not suggesting that you should go peeking beneath the bonnet of Rails but if you ever find yourself wondering how some of that Rails magic works then it is largely thanks to the flexibility of Ruby.

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)
The first two lines will both work. It doesn't matter which order the hash elements come in because hashes are unordered. You don't even need the {} because Ruby is smart enough to see that :price isn't part of the hash. The third line is an illustration of how the same thing might look in PHP. Those extra commas are there because validates_numericality_of actually accepts a whole bunch of other options. With a hash, you don't need to specify them all. You don't even have to know about them. So you see why this is easier? This isn't really something specific to the Ruby language, you could use PHP in this way if you wanted, though it might not look quite as pretty.

Hope this helps get you started. :)
Last edited by Chewi on Mon Sep 08, 2008 2:23 pm, edited 1 time in total.
User avatar
Mik
Born under a bad sign
Posts: 3394
Joined: Mon Jun 14, 2004 8:44 am
Location: Beyond your borders
Contact:

Post by Mik » Mon Sep 08, 2008 3:25 pm

tl;dr
User avatar
kaos
Noble Warrior
Posts: 4089
Joined: Mon Jun 14, 2004 3:09 pm
Location: Atlanta, Ga

Post by kaos » Mon Sep 08, 2008 6:03 pm

it can also look like this.

Image

ruby on rails
sounds like a folk song or slang for diarrhea
nik-w
Scavenger
Posts: 15
Joined: Fri Aug 15, 2008 11:31 pm

Post by nik-w » Tue Sep 09, 2008 1:03 am

As I said, brain fade re: the new topic thing!:p Had a look at those sites - it's certainly an interesting language! The "why's guide" thing is quite interesting at first, but I started skipping through the waffle after a while and just trying to read the code and figure it out! I think the best thing I can do is to venture manc-wards tomorrow (could do with a few things from town anyway) and see what they've got knocking around in Waterstones - I just checked their website to see what they had in, but the handy stock locator thing was clearly far too good an idea, so they've removed it. With a bit of luck, one of the two Waterstones will have something useful in stock!
User avatar
Chewi
Anti-Hero
Posts: 3521
Joined: Sat Jun 12, 2004 3:51 pm
Location: Edinburgh, Scotland

Post by Chewi » Tue Sep 09, 2008 8:26 am

I forgot to mention that there is also a Rails book called Agile Web Development with Rails. I have the first edition and I was going to say that even the second edition is probably quite outdated now but the third edition is coming out next month. There are a lot of other Rails books but make sure you get an up to date one. Rails changes a lot, adding extremely useful features all the time, and I have to look at a blog or two every now and then just to see what they've added.
User avatar
Chewi
Anti-Hero
Posts: 3521
Joined: Sat Jun 12, 2004 3:51 pm
Location: Edinburgh, Scotland

Post by Chewi » Tue Sep 09, 2008 1:42 pm

Heh, I just realised that the :minimum and :maximum stuff should have been :greater_than and :less_than so even I still have to look at the manual sometimes. :D
nik-w
Scavenger
Posts: 15
Joined: Fri Aug 15, 2008 11:31 pm

Post by nik-w » Tue Sep 09, 2008 6:00 pm

Chewi wrote:I forgot to mention that there is also a Rails book called Agile Web Development with Rails. I have the first edition and I was going to say that even the second edition is probably quite outdated now but the third edition is coming out next month. There are a lot of other Rails books but make sure you get an up to date one. Rails changes a lot, adding extremely useful features all the time, and I have to look at a blog or two every now and then just to see what they've added.
I went into town today - not one bookstore in town had a copy of the pickaxe book, "programming ruby" (I love the O'Reilly "programming x" books), or anything else similar. I went to all the mainstream bookstores as well as going deep into student territory and looking at studenty bookshops. I ended up in the larger Waterstones, as the smaller one doesn't actually appear to have a science section any more (the area where I thought was Computing is now "Pregnancy, Childbirth, & Sex" - nice!:p) which had a couple of Ruby books (notably "Learn Ruby" and "Ruby Cookbook") and several Rails books. Even the Rails beginners books made absolutely no sense to me - it just reminds me of Java (which I still couldn't get the hang of after 6 months of trying to understand it at uni - all my assignments were done by a friend in exchange for me doing his C assignments). Java freaked the hell out of me - I actually could not grasp the basic concepts of how to actually do *anything* with it. Even Lisp made more sense than Java, and Lisp is about the most backwards convoluted damned language in existence!:p

Plus all the books I read had a habit of slagging off PHP, which I found most off-putting. The whole concept seems to be to do all the programming in one file and have another file with the HTML and one-line function calls - the thing I like about PHP is that you can drop in and out of PHP as you need. To have two files to create one webpage is a bit alien to me - sure, I'll call library files from within several webpages, but only for common routines. To have one big file with all the programming for the entire site or a code file for every HTML file (plus whatever library files) is just odd!

I've installed mod_ruby on apache - god only knows if it does what I assume it does (basically to allow me to use ruby to write webpages like with perl (without having to rely on this rails business)) or how even to use it, but it's early days yet! I ended up buying the ruby cookboox, as I learn best by looking at code and then reading an explanation afterwards if necessary. I figured I could always use the link to version 1 of the pickaxe book alongside it & then maybe buy the newer version at some later date.
User avatar
Chewi
Anti-Hero
Posts: 3521
Joined: Sat Jun 12, 2004 3:51 pm
Location: Edinburgh, Scotland

Post by Chewi » Tue Sep 09, 2008 8:49 pm

I was lucky and bought both books from the O'Reilly stall at LinuxWorld 2005 in London. :)

I'm not sure why Java scares you so much. I found C quite scary to begin with, though I must admit I prefer C now. C has much fewer concepts to grasp but the lack of a safety net and having to do string manipulation with pointers makes it quite unapproachable.

To be honest, I could slag off PHP for hours but try not to take it personally. ;) It's not so much the language that they're slagging off because PHP can be made to work in the same way - PHP on Trax was started but I think it's dead now. It's all about the approach. I found the MVC concept quite alien at first too but now I don't miss the old ways at all. I've had to work on two PHP projetcts since starting with Rails. The first used that old approach and, by god, it was painful. The second was built around a pre-Rails bespoke MVC framework and it was so much nicer to work with. I won't go into the reasons why it's better because any good book will tell you. I'm not saying Rails is the be all and end all because it isn't but give me MVC over inline code anyday. Even for a single page with a form on it...

Last week, I had to construct an online questionnaire for a client. I didn't use Rails because it's overkill for a single page. Instead, I used Camping. It's a tiny framework, written by why, which takes ActiveRecord (the best part of Rails) and replaces the rest with something slightly Rails-like but very minimalistic. The entire Camping codebase (minus ActiveRecord) is only 4KB. The idea is that everything does go in one file but not in the inline way that you're used to.

Remember that Ruby is a general purpose language. It wasn't created for the web like PHP was. PHP can be used away from the web but it rarely is. While it probably is possible to use Ruby in a PHP-like way with purely inline code, nobody ever does. Since Camping does at least use single files, it might work for you as a stepping stone to the full MVC concept.

Don't use mod_ruby. Everyone was running away from it before I even started using Rails. It's old and has security issues, among other problems. For messing around, just use Mongrel. It doesn't need a separate web server and it just works with no configuration. For production enviroments, most people also use Mongrel but proxy to several instances of it via another web server like Apache, lighttpd or nginx. I think the reason you can't use Mongrel on the front line of a production server is because it's not multi-threaded so it will only handle one request at a time. I used to use FastCGI rather than Mongrel but I've moved away from that now.
Last edited by Chewi on Tue Sep 09, 2008 8:53 pm, edited 1 time in total.
nik-w
Scavenger
Posts: 15
Joined: Fri Aug 15, 2008 11:31 pm

Post by nik-w » Wed Sep 10, 2008 12:37 am

I'm gonna remove mod_ruby as I have no idea how to make the bloody thing work!:p The apache config basically implies that whenever apache comes across a .rbx file, it'll give it to mod_ruby. I created foo.rbx which just contained 'puts "foo!"' - ran "ruby foo.rbx" - no probs. Tried to access it in the browser and I just get "403 - Forbidden" - I put "#!/usr/bin/ruby" at the start, and still nothing. Grumble!:p

So basically, ruby without rails is just a perl/python alternative. It has some nice features - I am loving the fact you can do things like "foo.length" to find the size of a variable without having to think "what's the string length function again?"! I'm certainly going to carry on playing with Ruby and will probably have a crack at rewriting a couple of simple perl scripts just to get going on it! My favourite language has always been perl - the only reason I started using php was that perl/CGI became a bit of a nightmare for big scripts!:p Too many "Print <<END" statements. For anything non-web-based, I instantly turn to perl! Even things that most people would use bash/sed/awk/etc for, I'll use perl!:p Overkill, perhaps, but I know it and I like it. When I come to write something, it's because I need to do a specific task there and then, so I don't have the time or the patience for buggering about with things!:p

Java just made absolutely no sense to me at all - no matter how many times I looked at it, I just couldn't work it out - all this public/private/static/void/etc seemed so over-the-top for web stuff when you have languages like PHP and perl that sort it all out for you, and I couldn't work out any advantage over C/C++. I struggled with C++ at first (but then I'd never come across OOP before and was used to writing in Pascal, Modula2, & to some extent C) but I eventually got the hang of it to some extent.

I like the simplicity of languages like PHP - I don't see what's wrong with basically writing a webpage with bits of code stuck in it. Maybe it's different for complex websites (e.g. something along the lines of Amazon or what have you), but I've never written a page where I've thought "this is getting messy" - because any repetitive stuff I'll just stick in a function and drop into an include file. I considered learning ASP at one point, but then I couldn't see much point in that - it came across too much like Microsoft seeing languages like PHP/Perl/Java and going "we're going to make our own just so it gets along well with IIS (which is one of the most evil inventions ever), and despite primarily using Windows, I am not a Microsoft fan. You can't argue with the success they've had, but unchecked monopolies often are successful;)

I guess this is why I made a career out of programming and write bits of webpages for myself, friends & family!:p Sad thing is, almost everywhere I go and everything I do, I think "this would be much easier and quicker if I could just write a perl script to automate it":p
User avatar
Chewi
Anti-Hero
Posts: 3521
Joined: Sat Jun 12, 2004 3:51 pm
Location: Edinburgh, Scotland

Post by Chewi » Wed Sep 10, 2008 1:18 am

If Perl's your thing then you should like Ruby because some of the best bits were included. For instance, the match variables like $3 and $`. Some of the iteration stuff is also similar. I was never a big Perl fan (I learnt PHP shortly afterwards and got hopelessy confused in my Perl exam) but I live for regular expressions so I do appreciate that part of it. I always use bash/sed/awk/grep over Perl though. I learnt awk just recently. :D As for PHP vs Ruby, it's also nice to ditch that preg_match crap in favour of stuff like foo =~ /bar/.

If you REALLY want to do inline stuff then check out this page. eRuby is the C extension equivalent of the pure Ruby erb library, which is what Rails uses for its view templates. It can be hooked directly up to Apache. If you're going to keep coding this way though, you might as well stick with PHP. Stick it out with Rails a little bit, I'm sure you'll come around. ;)

If OO scares you then don't focus on that side of Ruby too much, at least for now. For basic Rails stuff, it's not very important. The only major Rails feature I can think of that specifically relies on it is STI - that's single table inheritance, not sexually transmitted infection. :P

The bottom line is I guarantee this will make you a better coder. I don't know what your PHP code is like (I'd like to see some) but I know that my pre-Rails code was not just ugly but also full of holes you could fly a plane through. Security is at the top of my agenda now and these days, you can't afford to be complacent about it. Some members of this forum will remember this. :P
Last edited by Chewi on Wed Sep 10, 2008 1:20 am, edited 1 time in total.
nik-w
Scavenger
Posts: 15
Joined: Fri Aug 15, 2008 11:31 pm

Post by nik-w » Wed Sep 10, 2008 3:10 pm

I've just been talking to my best mate from uni (we learnt to program C/C++/PHP together - he also taught me more Perl than the lecturer ever did) - I was telling him about my confusion about the whole MVC thing and he's pretty much explained it to me a bit clearer (he knows the sort of crap I write!:p) - by comparing it to perl! Seems I was looking at a similar thing some years ago when I was looking at Perl Template Toolkit - it's not identical, but you have perl code which creates templates using Mason, does stuff, passes it to another process, which then deals with putting it into the template. Probably a simpler system than full-blown MVC, but at least I have more of an idea what it's actually about now!:p

I'll send you a PM with some links to a few examples of the kind of crap I write!:p
User avatar
Chewi
Anti-Hero
Posts: 3521
Joined: Sat Jun 12, 2004 3:51 pm
Location: Edinburgh, Scotland

Post by Chewi » Wed Sep 10, 2008 3:19 pm

The last bit of PHP I wrote before starting with Rails (was quite a gap, didn't write any web stuff in my final uni year) made use of the Smarty template engine so I had already recognised that my code needed to be cleaner though I was still oblivious to the fact that it was full of holes. I found Smarty to be very restrictive though. Like most template engines, it uses a simplified custom language for the templates and you always find yourself wishing you could just write some plain old PHP in there, even if most of your code exists outside of the templates. One of the great things about Rails is that it uses Ruby throughout, from the database calls (though you can write some custom SQL if you need to) to the view templates. It doesn't restrict you in the way Smarty does.

I should add that one of the arguments for using a custom template language was that web page designers didn't need to learn PHP or whatever. This is a fair point but DHH argued that Ruby (at least at the web page level) is just as readable and easy to learn as any custom template language. There are a lot of Rails helper functions to remember but you don't have to use them - it really pays off if you do though!

Looking forward to this PM. :D
Last edited by Chewi on Wed Sep 10, 2008 3:25 pm, edited 1 time in total.
nik-w
Scavenger
Posts: 15
Joined: Fri Aug 15, 2008 11:31 pm

Post by nik-w » Wed Sep 10, 2008 3:26 pm

Chewi wrote:Looking forward to this PM. :D
It's been sent - feel free to have a look round, just try not to laugh too hard!:p
User avatar
Chewi
Anti-Hero
Posts: 3521
Joined: Sat Jun 12, 2004 3:51 pm
Location: Edinburgh, Scotland

Post by Chewi » Wed Sep 10, 2008 3:29 pm

Hmm I didn't get it. Should be instant.
nik-w
Scavenger
Posts: 15
Joined: Fri Aug 15, 2008 11:31 pm

Post by nik-w » Wed Sep 10, 2008 3:33 pm

Chewi wrote:Hmm I didn't get it. Should be instant.
Grumble. Should have clicked "save to sent folder"!

Will retype/resend!
Post Reply