Search code examples
ruby

How do people at Ruby think they will get away from static members? (Also: Tips for Java immigrant)


I was on #ruby on Freenode getting some real advice on how Ruby is different from Java yesterday, when someone pointed out that Ruby has no static members. I was really surprised.

Can anyone tell me what's is the substitute for using static methods (static variables can be duplicated using @@)(Someone told me to use a better design in the program rather than using archaic concepts like static functions.)

PS: Are there any other sources/tips/links you would recommend for a Ruby learner with prior Java experience?

Currently on: Beginning Ruby page 36.


Solution

  • tl;dr answer: Give it time and embrace the differences, it will come to you eventually.

    Are there any other sources/tips/links you would recommend for a Ruby learner with prior Java experience?

    Yes, forget want you learned so far and start from scratch. I've been teaching Ruby courses on RubyLearning.org since January 2009 and regularly it's the people with only a Java background that have the biggest trouble adjusting to Ruby. Since I don't have much Java experience, I can't really say why that is, but the observation holds in every single new course.

    It just seems to me that many Java developers assume that the way things are done in Java is the only way/the right way for some reason. But in a lot of regards programming languages are like real languages. Some are very close and concepts (and grammar) translate easily between them. Others are very different, but that doesn't mean they are not able to express the same things. Asking "how do you get away without static members" is like a German speaker asking a Chinese person how they get away without using grammatical cases, it's just the way the language works. Generally it's a bad idea to say "programming language X has this, how do I do that in language Y", but try to find out how things are generally done in Y.

    After this rant, here are some more practical tips:

    If you are interested in books, I usually recommend "The Well-Grounded Rubyist", followed by "Eloquent Ruby" and "Ruby Best Practices". I blogged about this not too long ago:

    http://citizen428.net/blog/2011/04/23/ruby-book-recommendations/

    Another thing I'd recommend is writing a lot of code, be in with the Ruby Koans, programming katas or contributing to an open source project. While doing so try to let Ruby's features influence the design, not your Java program. This may be hard at first, but I've seen people improve a lot in a really short time (our courses are 2 month long) by embracing the differences between the languages instead of forcing one to be like the other. Programming is a way too think about and solve problems, languages are just tools. You've obviously learned the first already, now try to apply it to different languages, following different paradigms.

    You may have heard that it's good learn one new programming language per year. I don't particularly care about the time interval, but learning a new language is quite likely to make you a better coder in some way, as it often forces you to tackle problems from a new angle, thus increasing your "problem solving vocabulary".