This article was originally published on my website jakenherman.com. Cover Image by Elegant Themes.
The best way to learn any language is to simply just use it.
The best advice that I can offer would be to learn algorithms, design patterns, and programming fundamentals. Once you have learned these things, you can write virtually any program in virtually any language.
For example - I do not know how to use the language Ruby. However, I am about to walk you through the popular FizzBuzz program in Ruby by using my knowledge of programming fundamentals (loops, conditionals, operators, io, etc.):
The first thing I need to do is define the problem. This is the first step to writing any program. If you don't first know what the problem is, you cannot find a solution to it. The problem is as such: For each number from 1 to 100 I would like to print "Fizz" if the number is divisible by 3, "Buzz" if the number is divisible by 5, or "FizzBuzz" if the number is divisible by both.
The next thing I need to do is check the Ruby documentation for using a "loop":
a = 0 until a > 100 do a += 1 end
Now that I have an
until loop, I need to check how to do modular arithmetic in Ruby by once again, checking the documentation, as well as checking how to call
if statements, then applying what I've found, modifying my code as such:
a = 0 until a > 100 do if a % 3 == 0 print fizz end if a % 5 == 0 print buzz end a += 1 end
Lastly, you'll notice I just wrote "print fizz" and "print buzz" in my if statements. Now I need to check how to write my results via the documentation, and modify my code as such:
a = 1 until a > 100 do if a % 3 == 0 print "fizz" end if a % 5 == 0 print "buzz" end a += 1 puts "" end
There we go - my first ruby program has been written. You saw my thought process as well as my methods on finding out how to get the program done. Now I can think of a more difficult or complex program, and use the same methods to write it in ruby. In time, just by using ruby, I will have learned it.
Also keep in mind that this program is likely not the best way of handling things - there are improvements to be made. That is okay. Nobody will expect you to write perfect code while learning.