Vital Ruby concepts to learn before I jump into Rails? [closed]
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated an开发者_如何学编程swers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this questionDisclaimer - Ruby is the first language I've ever learnt. I don't have any CS background.
I've worked through "Learn to Program" by Chris Pine which has been enjoyable and I now understand the basics pretty well. The next recommended book would be "Programming Ruby 1.9" (the next PickAxe). From what I've read it's just a massive reference book but it's 960 pages and that scares me.
What, in your opinion, would be the next 'gotta-know' concepts that I should learn?
Thanks in advance.
Blocks and a bit of metaprogramming.
A good starting point (it was that for me...) are the Ryan Bates screencasts on Ruby on Rails: http://railscasts.com/ (start from the lowest numbers first...)
I checked the tutorial you read. There is one very important concept that is missing: symbols. You can find information in "ruby learning". They are used a lot in rails as option names.
Another concept that puzzled me when I tried reading rails examples was the fact that, when an hash is the last argument of a function, curly braces can be ommited. So:
def foo( 1, "blah", "a" => "b", "c" => "d" )
should read as:
def foo( 1, "blah", { "a" => "b", "c" => "d" } )
The function foo
takes only three arguments.
Have a look at Humble Little Ruby Book(free) to be more familiar with other few concepts.
Another book will be Ruby for Rails.
You don't actually need to know anything fancy about Ruby to be able to put together a Rails site. I started out just hacking my site together, noticing nice language constructs and seeing what they do in the source code of plugins and gems I was using.
The gratification of building something quickly in Rails will keep you going.
There is more than enough in terms of Rails conventions to learn to keep your mind busy and getting advanced in Ruby is not going to be a priority for some time.
Go through the Rails guides http://guides.rubyonrails.org/ to start building functionality.
- Blocks related stuff (blocks, Proc, lambda)
- Modules
- method_missing
The big thing for me was what self means at different points in a class definition, and the idea of instance variables vs class variables vs singleton variables.
class Foo
@variable # is an instance variable on the class object, not on instances derived from this class
def bar
@variable2 # is on instances derived from this class
end
def self.baz # this is a class level method, similar to static methods in something like java
@variable3 # gets defined on the class object again
end
end
f = Foo.new
def f.wtf #this method gets defined only on this single instance of Foo
@variable4 #this variable will only be for the f instance
end
This sort of thing tripped me up in rails all the time before I got good at ruby, but grokking the idea of class objects, object individuation, and what self
was pointing at in any given place was hands down the hardest thing for me to learn.
The most fun I've had reading about a programming language was _why's Poignant Guide to Ruby.
It's hilarious, artsy, and well written. Also, there's an intro to Ruby in the Appendix of "Agile Development with Rails." That seemed to have just the right amount of programming intro to get started.
https://en.wikipedia.org/wiki/Why%27s_%28poignant%29_Guide_to_Ruby
http://poignant.guide/
精彩评论