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:
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 | 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With