开发者

How can this C and PHP programmer learn Ruby and Rails? [closed]

开发者 https://www.devze.com 2022-12-23 01:37 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago.

I came from a C, php and bash background, it was easy to learn because they all have the same C structure, which I can associate with what I already know.

Then 2 years ago I learned Python and I learned it quite well, Python is easier for me to learn than Ruby. Then since last year, I was trying to learn Ruby, then Rails, and I admit, until now I still couldn't get it, the irony is that those are branded as easy to learn, but for a seasoned programmer like me, I just couldn't associate it with what I learned before, I have 2 books on both Ruby and Rails, and when I'm reading it nothing is absorbed into my mind, and I'm close to giving up...

In ruby, I'm having a hard time grasping the concepts of blocks, and why there's @variables that can be accessed by other functions, and what does $variable and :variable do? And in Rails, why there's function like this_is_another_function_that_do_this, so thus ruby, is it just a naming convention or it's auto-generated with thisvariable _can_do_this_function. I'm still puzzled that where all those magic concepts and things came from? And now, 1 year of trying and absorbing, but still no progress...

Edit: To summarize:

  1. How can I learn about blocks, and how can it be related to concepts from PHP/C?
  2. Variables, what does does it mean when a variable is prefixed with:
    • @
    • $
    • :
  3. "Magic concepts", suchs as rails declarations of Records, what happens behind the scenes when I write has_one X

OK so, bear with me with my confusion, at least I'm honest with myself, and it's over a year now since I first trying to learn ruby, and I'm not getting younger..

so

I learned this in Bash/C/PHP

solve_problem($problem)
{
  if [ -e $problem == "trivial" ];
    then
      write_solution();
    else
      breakdown_problem_into_N_subproblems(\;
      define_relationship_between_subproblems;
      for i in $( command $each_subproblem );
        do
          solve_problem $i
        done
  fi
}

write_solution(problem) {
  some_solution=$(command <parameters> "input" | command);
  command | command $some_solution > output_solved_problem_to_file
}

breakdown_problem_into_N_subproblems($problems) {
  for i in $problems;
    do
      command $i | command > i_can_output_a_file_right_away
    done
}

define_relationship_between_subproblems($problems) {
  if [ -e $problem == "relationship" ];
    then
      relationship=$(command; command开发者_StackOverflow中文版 | command; command;)
    elsif [ -e $problem == "another_relationship" ];  
      relationship=$(command; command | command; command;)
  fi
}


In C/PHP is something like this

solve_problem(problem)
{
  if (problem == trivial) write_solution;
    else {
      breakdown_problem_into_N_subproblems;
      define_relationship_between_subproblems;
      for (each_subproblem) solve_problems(subproblem);
    }
}

And now, I just couldn't connect the dots with Ruby, |b|{ blocks }, using @variables, :variables, and variables_with_this_things..


You said that you are reading books, so probably what you need is:

Read less, write more.

I'm also from C background and many concepts of ruby was completly alien for me and still I don't understand them all. But instead of only reading I decided to write and now I have few programs/scripts and webpages (even quite big) and I really love ruby. When you write you will start to "feel" all this concepts. Use irb to test what you read from books. Write some methods with blocks even if you don't understand them and play with it.


In addition to the other answers given so far, you might want to brush up on basic object-oriented programming. The concept of "instance variables" is very important in a language (like Ruby) where everything is an object.

Then, you might want to look in to functional programming, where the idea of functions as values finds its roots and realize that a block is nothing more than a chunk of code that can be executed arbitrarily and with different arguments, but can also access variables defined outside of it through the magic of something called a "closure".

Wikipedia's not a bad place to start; the articles on object-oriented programming and first-class functions will introduce these concepts.


To understand Ruby and Rails there are a couple of steps you could take such as:

  • Read the Rails Guides which are well documented and cover the essentials and details of the architecture of the framework. Experiment with the examples which are included.
  • Have handy the Rails Framework documentation, which also includes a lot of information in the readme parts or header parts of many of the pages. The find feature of your browser will become your friend when navigating all the information stored in the documentation. This can also be supplemented with learning how to use the gem server command to fire up the documentation on your local workstation.
  • Have the Ruby API language guide (Core and Library)

Now this is will give you a good set of reference material. As with learning any new language or framework often the biggest issue is knowing where the information is located. And when you hit a problem with something in the application you are building you don't need to be wrestling with the question Where did i read, see, find that bit of information that I need.

The next set of information that is useful is more what I'd call application and logistics type of information. This includes;

  • Read the Programming Ruby guide so you know the basics of the language used to write the Rails framework.
  • Read Agile Web Development which gives you not only insights into the framework but takes you through the process of building an application in a very agile approach.
  • Take a look at the Advanced Rails Recipes which will then extend your knowledge.
  • Learn the tools that come with the framework such as the console, rake, gems and extensions
  • Lastly find a small project, maybe something on GitHub, that you can review the code. It should be an application which is not too large so that you don't get swamped in the detail, yet a sufficient size so you can see how all the parts work together.

Hope that helps.


Read these books, and when you have trouble grasping a concept ask individually.

The ruby Programming Language: http://oreilly.com/catalog/9780596516178
The Ruby Way: http://rubyhacker.com/toc.html
Ruby for Rails: http://www.manning.com/black

I'll answer question no 2 as the other two are vague and probably they would need a few chapters
vars: Ruby vs C++

@var  similar to this->var 
@@var similar to SomeClass::var (static)
$var  similar to global in c

A :symbol in ruby is a unique numeric identifier, the interpreter converts all symbols in your program to unique integers. They are useful as indexes (faster than strings and more readable than integers): hash[:user_name]


Maybe you should break some of specific concerns out into questions?

For example:

@variables are object instance variables - values are local to specific instances of an object and hence accessible from all methods.


You really should read a decent book on Ruby. The best one, which is also considered as the reference book in the Ruby community is the PickAxe book: http://www.pragprog.com/titles/ruby3/programming-ruby-1-9

This is the third edition of Programming Ruby and covers Ruby 1.9. The second edition of the book covers Ruby 1.8. The first edition covers Ruby 1.6. And the first edition is freely available online at: http://www.ruby-doc.org/docs/ProgrammingRuby/

Reading such a book will teach you the basics. Maybe a lot of the stuff you will already know, but since you ask such basic things as :variable (something that starts with ":" is a literal, not a variable) and @variable, I really urge you to read a book.


  1. Don't try to learn Rails, try to learn Ruby first.
  2. There's no blocks in PHP or C, but I'm gonna explain a little bit here:

    You can think in blocks just like name-less functions, ie.

           my_array = [1,2] 
           my_array.each { |parameter| print(parameter) }
           my_array.each do |parameter| 
              print(parameter)
           end 
          

       the Array#each method sends, on each iteration, a parameter to the block, 
       in this case is the value of each element on the array.
    
       the DEFinition of Array#each could be:
        <pre>   
           def each(&block)
              block.call(elem_of_the_array) 
              #the block will catch "elem_of_the_array" as "parameter" argument
           end
        </pre>
    
       in PHP you can write this block as function like this:
       <pre>
       function name_less_function(parameter){
            print(parameter);
       }
       </pre>
    

    the real concept behind the blocks are Procs, do research about them.

  3. Learn Object-oriented programming, maybe this post can help you => http://raflabs.com/blogs/silence-is-foo/2009/12/13/the-ruby-object-model/


Don't try to learn everything at the same time. Focus your efforts and build incrementally. Minimize the confusion by doing the following one step at a time:

  1. Master the [basic] concepts of object-oriented programming.
  2. Master the [basic] concepts and syntax of Ruby. Don't even worry about Rails at this stage.
  3. Master the [basic] concepts of the Model–View–Controller (MVC) architecture.
  4. (Optional) Build something using an MVC framework for PHP (like cakePHP or CodeIgniter). This way, you will see MVC concepts in action and in a language that you already know.
  5. Rails time! There are plenty of books, guides, tutorials, podcasts and screencasts about Rails, both free and paid. Look around for a recommended resource of your favorite type and use it to learn Rails.

On a side note, I personally don't think that Rails is that easy to learn (which is a subjective matter anyway). IMHO, it has a considerable learning curve, but once you get over it, you may well find yourself more productive at web development. That is, you may well find Rails to be easy to use.

Some general tips:

  • Reading alone is not enough to truly master a programming language/framework. You have to write code.. a lot of code. Don't just follow the example application of your learning resource of choice. Look at Project Euler and similar websites for different and probably more entertaining challenges.
  • irb is your friend. Use it as much as you can.
  • If you can't figure something out, search. If you still can't figure it out, post a question on Stack Overflow or Ruby Forum. Just make your question specific.
  • Learning takes time. Don't get frustrated and take things easy.


How can I learn about blocks, and how can it be related to concepts from PHP/C?

Blocks could be seen as lambda functions (as you noted you got some Python experience), they are kind of anonymous.

@

This is a local instance variable. If you would use @@, you get a class-wide variable. For i.e. @@number_of_all_created_classes and @my_var_that_is_for_this_instance_only

$

This is a global variable.

:

Ah, people will flame me for this: it's kind of a pointer. The : operator allows you to use a string reference to point to an object.

"Magic concepts", suchs as rails declarations of Records, what happens behind the scenes when I write has_one X

has_one, belongs_to, etc., are relative functions to cross reference. They won't relate to C or PHP for that matter, but if you have done some serious PHP/MySQL programming and did some relation mapping it will spring up. You might want to read up about ORM.

Currently I am reading "Agile Web Development with Rails 2.0", which is awesomeness. Also the guide written by Mr. Neighborly's Humble Little Ruby Book (is available online) is something quick and good for you to read.

I thought I'd answer your questions more directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号