Design Patterns in Rails - ruby-on-rails

I remember, I have a GoF book back in college about design patterns which helped me a lot with my C and C++ programming, since my jump ship to Rails I was trying to use those design patterns I learned previously, Rails is a relatively new paradigm to me, plurals, verbs, REST, DRY.. Can you give me a recommended book for Rails that I can easily understand what I previously learned back in College.
P.S. I suspect Matz knew about the GoF book, and applied it on Ruby... :-)

Topics such as REST, DRY etc are not really in the same category as the patterns covered by the Gang of Four. There is a book, Design Patterns in Ruby which you might find useful as it covers how the original GoF patterns apply in Ruby. However, once you start programming in dynamic languages like Ruby you often find you don't need patterns in same the same way as you did in languages like C++ and Java.
I would also take a look at The Ruby Way (for general Ruby programming), Agile Web Development with Rails (for a good introduction to Rails) and The Pragmatic Programmer (which covers many of the general practices such as Don't Repeat Yourself.)

You shouldn't start learning framework without learning the language first. But if you feel confident you can check out "Agile Development With Ruby On Rails". Pragmatic bookshelf (my favorite) has some neat books on RoR development too.

Related

In Ruby/Rails, what is "Rails"?

So I am leaving a job that is solely Java stack, and moving to one that doesn't outwardly admit it, but they are absolutely Ruby/Rails crazy.
I understand Ruby fine, but "Rails" doesn't seem to be much more than an MVC framework which the Ruby language is placed upon.
I'm sure there are plenty of people rolling over right now wondering how I could be so naive, but seriously... please help me understand what Ruby is w/o Rails.
Willing to take any readings or books suggested! Sorry, I'm new to the game! Thanks!
Ruby is a language, much more closely related to Perl or Python than to a C-based language like Java. Ruby is very approachable as a language, and there's even an interactive console that you can use to quickly experiment with. Ruby itself is interpreted, so you have a lot of dynamic capabilities at your disposal. It's really a good language that you can pick up quickly.
As you say, Rails is a MVC framework that includes an ORM (ActiveRecord), a view template engine (ERB), and a variety of extensions to the standard Ruby library. Most Rails applications are websites, but you can use Rails for other purposes, such as an API-based headless server or a mobile application server - these are often combined together to make full-stack applications.
I typically recommend 2 books to programmers looking to make the switch:
Programming Ruby by Dave Thomas, Chad Fowler, and Andy Hunt
Agile Web Development by Sam Ruby, Dave Thomas, and David Heinemeier Hansson
"Programming Ruby" covers the language constructs and use of standard tools with decent examples in the first half of the book, followed by a standard library reference in the second half. It's well written, and loved by millions as 'The Pickaxe Book'. Make sure to get the Ruby 1.9 & 2.0 book, as this covers the most recent Ruby versions and features.
"Agile Web Development" covers the introduction to Rails by developing projects and explaining the problems and decisions made along the way. I've found this to be very helpful as an introductory book for programmers looking to make the switch from another language/framework.
Here is a books list
Also Ryan Bates maintained an awesome screencats collection
Official RoR guide
I hope that helps.
I think it's important to note that Rails is not just like any other web framework in another language.
Rails is unique in that it is a "Domain Specific Language" that uses the metaprogramming features of Ruby, its host language, to modify that language substantially. There are many methods added to standard Ruby classes such as Hash and numeric classes.
Also, there are several implementations of the Ruby language, the two best ones being "MRI" Ruby (1) and JRuby (2).
(1) MRI = Matz Ruby Implementation; Matz is the nickname for Yukihiro Matsumoto, the primary author of the Ruby language. This implementation uses C language for low level operations and is sometimes called "C Ruby".
(2) JRuby - Ruby implemented in Java and running on the Java Virtual Machine, enabling interacting with code in Java and other JVM languages, and enabling use of all available CPU's for threads, unlike MRI Ruby which is limited to 1.
Along with the resources listed above, here is another tutorial/book that is free which is a staple in learning Ruby on Rails: https://www.railstutorial.org/book
It starts with many beginner concepts to more advanced concepts. It's super easy to understand.
It also teaches using Test Driven Development, which a lot of rails developers advocate; another perk to trying this book.

Rails patterns specific to web application

I know about GoF and Rails patterns. But I am dying to learn more about common Rails patterns that apply specifically for web-app. For example, a SignUpPresenter would be great for a form that handle multiple models in one form. It took me about a month to realize this b/c I can't find a good source.
Thank you.
For patterns and anti-patterns I highly recommend Rails AntiPatterns: Best Practice Ruby on Rails Refactoring
It's not all about patterns but you'll discover quite a few of them there. This book is also quite an easy read.

Rails learn's confusion

This is a beginner's rails learning confusion. When I learn rails, from time to time, I feel frustrated on rails' principle "Convention over Configuration". Rails uses heavily on conventions. A lot of them are just naming conventions. If I forget a convention, I will either use the wrong naming and get unexpected result or get things magically done but don't understand how. Sometimes, I think of configuration. At least configuration lists everything clearly and nothing is in fog. In rails, there seems a hidden, dark contract between you and the machine. If you follow the contract, you communicate well. But a beginner usually forgets items listed on the contract and this usually leads to confusion.
That's why when I first pick up rails, I feel like it is somehow difficult to learn. Besides, there are many other things that could be new to a learner, such as using git, using plugins from community, using RESTful routing style, using RSpec. All these are new and come together in learning ruby and rails. This definitely adds up difficulties for a beginner.
In contrast, if you learn php, it wouldn't be that bad. You can forget many things and focus on learning php itself. You don't need to learn database handling if you know SQL already(in rails, you need to learn a whole new concept migration), you don't have to learn a new decent unit test(in rails, usually they teach RSpec along the way because rails is agile and you should learn test-driven development in the early learning stage), you don't have to learn a new version control(in rails, you will be taught about git anyway), you don't have to use complicated plugins(in rails, they usually use third-party plugins in textbook examples! what the hell? why not teach how to do a simplified similar thing in rails?), you don't have to worry RESTful style.
All in all, when I learn php, I learn it quick and soon I start to write things myself. Learning php is similar to learning C/java. It tastes like those traditional languages. When I learn rails, it is more difficult. And I need to learn ruby as well (I believe many of you learn ruby just because of rails).
Does anyone have the similar feeling as I have? How do you overcome it and start to master rails? Hints will be welcomed. Thank you.
You do need to learn Ruby. Ruby is a language. It has its own quirks. You don't need to learn a lot of Ruby, but you can't learn no ruby and expect to write anything decent.
Next, you do need to learn the Rails conventions. Learning the conventions means that you don't need to learn the Ruby networking classes. If you don't want to learn the conventions, then writing anything of value will be difficult.
It's really no different than learning C# (as a language), then learning an MVC framework, like ASP.NET MVC, or FubuMVC.
Yes, you throw up a anything in PHP, learning a lot less in the process. But investment is valuable. Your education is valuable.
Learning takes time. Modelling is important, and I've seen some ugly Rails code because the authors were poor modellers. Those poor models turned into ugly databases. But it still worked! It met that customer's needs.
You comparision Rails with PHP does not hold, since Rails is a framework and PHP is a language. You should compare Ruby with PHP, or Rails with some PHP framework (I am not familiar with PHP frameworks and do not know an example)
But you are right about the things that 'magically' happen with Rails. I too had problems understanding them and looked many things in the Rails code to see what was actually happening and why.
To learn Rails you should first learn the language on which it is build (like you should do with any framework IMHO), so learn Ruby first. Personally I liked Mr. Neighborly's Humble Little Ruby Book a lot. It is written in a easy and nice way and is not boring/serious like a lot of books on languages.
After that I read/did all tutorials/guides on http://guides.rubyonrails.org/ they covert the (important!) basics and tell about the Rails specific things you are writing about.
When I first heard of Rails the claim was that you could build a web app ten times faster than the other frameworks. I always thought this was an exaggeration.
Most of the conventions are hard to remember if you don't have ten years of doing it the 'configuration' way. So, for someone new to web development it is hard to remember all the conventions and to understand why they are important. I definitely did not see a ten time increase in productivity. Routing, for example, probably started nice and simple but grew more and more complex. I think some effort should be made (perhaps by the people writing Rails books) to not try and throw every capability out there to confuse newbies.

Is there a "Code Complete" book for Ruby?

I began reading the book "Code Complete" 2nd edition, but stopped reading when I noticed most of the solutions were easily solvable in Ruby with Ruby idioms. Is there a similar book for Ruby?
Here's the version that I started reading:
http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670
Pick that book back up and start where you left off. As someone who read the first edition and these days writes Ruby for a living, I can confidently say that the lessons of Code Complete are universal. The wisdom in that book about good code construction - quality naming, decoupling, how to structure a function, etc. - will stand any programmer in good stead. I still refer to my dog-eared first edition regularly.
As far as books on practices which pertain more specifically to Ruby: first, every programmer should have a copy of "The Ruby Way, 2nd Edition", by Hal Fulton, on his or her desk. Second, while I haven't read it yet, I have heard good things about "Ruby Best Practices".
For such an old programming language (well, maybe not "Lisp" old, but Ruby is older than Java, after all) and a community that fanatic about code quality, style and beauty, the Ruby book market is surprisingly light on "higher level" books.
There are several reasons for this: a lot of Rubyists are already seasoned Smalltalk or Lisp programmers when they pick up Ruby, so they don't need those books. And Ruby and Smalltalk are similar enough that you can just read a Smalltalk book instead. (For example, you can pretty much read Stéphane Ducasse's list of free Smalltalk books from top to bottom.) The same goes for Perl.
Until very recently, the best higher level Ruby book was basically Smalltalk Best Practice Patterns by Kent Beck. (There is a newer version of this book, called Implementation Patterns which uses Java instead of Smalltalk. However, since Kent's Java style is very Smalltalk-ish, it is essentially SBPP, 2nd Ed.) However, since then, several higher level books have been released or are in the works:
Design Patterns in Ruby by Russ Olsen,
Ruby Best Practices by Gregory T. Brown,
Refactoring: Ruby Edition by Jay Fields, Shane Harvie, Martin Fowler and Kent Beck (a re-interpretation of Fowler's book in Ruby) and
Rein Henrichs is (or at least was, as of last year) working on Ruby Best Practice Patterns.
As stated in the comment above, I'm not sure what I should recommend.
However you might take a look at the Pragmatic Series with books like Practices of an Agile Developer or The Pragmatic Programmer if you dont know them yet. At least they drop the C parts, but unless you go for the explicit Ruby-stuff they will use languages like C# for demonstration.

How does ruby on rails work?

I'm a PHP developer who knows a little bit of Ruby. I want to learn Ruby on Rails, but most of the resources I've come across treat RoR functionality as "magic" -- i.e., it has a certain internal consistency, but don't bother asking how it works in terms of Ruby, MySQL, etc.
Anyway, I want a deep understanding of how RoR works, the design decisions that went into building it, etc. In particular I'm interested in ActiveRecord, but really I'm looking for the whole package.
Any books / sites / advice welcome.
The books "Agile Web Development with Rails" and "The Rails Way" are both pretty good. "Pro Active Record" goes really in depth for Active Record, but doesn't touch on too much else. The podcast Railscasts sometimes just uses the magic, but sometimes it explains what is really going on. Various blogs such as Art of Mission can get into what your looking for.
Additionally, using the ruby-debug gem gives you a much better understanding of what is going on - you can step into what is running behind the scenes.
There are two areas here that can be examined separately:
General concepts (which include design patterns, general attitude principles like DRY and CoC or even agility etc.) since they are the principles behind many design decisions in Rails. Any of these concepts can be studied independently of Rails (since they are general programming concepts). It's good to have at least some understanding of these before digging any deeper. There are many sources about such general principles all over the net (aforementioned Martin Fowler's site is one of the better sources for grasping such concepts).
The way Rails implements these concepts into its corpus. There are, again, two things to have in mind here. Since Rails heavily exploits features of its mother language, it's crucial to understand Ruby's peculiarities (such as mixins and plenty of Ruby's dynamic features). The last thing to understand is how Rails uses these features, ie. how it's broken down into classes and modules, how many of its features are dynamically created on the run and so on. For this purpose, I highly recommend book Ruby for Rails from David A. Black (which is exactly about Ruby, its peculiarities and the way Rails uses them). While this book might be a bit out of date, I think it's still invaluable in its content.
If you really want to know how it works, you can just look at the source code. The online API docs let you see the source code of every method right in the documentation page.
ActiveRecord in Rails is based on Martin Fowler's Active Record pattern. It's basically an ORM (object-relational mapper). It's not even really that sophisticated as far as ORMs go. Java Persistence with Hibernate has a good overview of common ORM concepts.
This one might fit here well too -> http://railsforphp.com/
This is sort of a tangential answer to your question, but I, too, came from PHP development over to Ruby/Rails dev, and it was a big transition for me. As PHP devs, we're used to getting into the nitty-gritty, to to speak, and not being prevented from looking under the hood.
By design, Rails is a black box. You're supposed to learn Rails, almost as a language itself. It's a new way of thinking of web dev, especially from a PHP dev's perspective, though this idea is not uncommon to programming in general.
Not bothering to look under the hood may be the best way to start doing things "the Rails way."
Jamis Buck has a nice series of "Under the Hood" articles on his blog that cover some aspects of Rails' internals.
The "magic" in Rails involves method_missing and the concept of metaprogramming. Dave Thomas of The Pragmatic Programmers created a set of screencasts on metaprogramming Ruby.
PHP has a somewhat-equivalent function called __call, and PHP 5.3 introduced __callstatic. You might take a look at those, as well as search Google for "php metaprogramming", for some reading material on implementing Rails-like behavior in PHP.
I found this site a good starting reference:
http://www.tutorialspoint.com/ruby-on-rails-2.1/index.htm
It assumes MySQL in its examples.
I am sure you can find heaps and heaps by just googling for "ruby on rails tutorials", though.
ruby metaprogramming magic in rails: look at books by Rappin "Professional Rails" and Ediger "Advanced Rails", in addition to Black "Ruby for Rails" mentioned above
Naming conventions are important:
http://rails.wincent.com/wiki/Rails_conventions
http://www.softiesonrails.com/2007/10/18/ruby-101-naming-conventions
If you really want to understand Rails, read the source for the bit of Rails that you want.
/Library/Ruby/Gems/1.8/gems/ on OSX or freeze rails and have access to it in your RAILS_ROOT/vendor/ directory

Resources