Subscribe to my Twitch channel for free using Amazon Prime

Subscribe to my Twitch channel for free using Amazon Prime. https://markramsey.com/2019/12/03/subscribe-to-my-twitch-channel-for-free-using-amazon-prime/.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




5 Things I learn that are useful in Ruby

Starting to code, in general, can be really challenging when you start.

Ruby is one of the language fit for a beginner to learn in the world of coding.

Here are the 5 things I use to improve my Ruby coding (mostly debugging) experience.

binding.pry save my a** more times then a pillow cushion saves my real a**. To get started on using binding.pry you need the following:

put this on the top of your code
and then place this to the place need to debug

What this does is when you run your code it will stop where you place binding.pry and allow you to see what variable has been placed, find out if the value is correct at that moment, etc. This is one of the most basic debugging technique that everyone coding ruby should know. It will help you. A lot.

Here is some command in pry you may not know about.

pry-nav

pry-rescue

Overloading method is pretty useful especially if you want all the method your code inhered but want to change one or any method. One good case for this is creating a game. In that game, you have an enemy that attacks or have a slightly different variation on each enemy. For example, default enemy deals one damage but you can inherit that and change the attack to 5 for boss type.

As you can see Boss inhered Enemy but change the attack to 5.
As so.

There is much application to use this but the most useful is probably games. It is also one principle of Object Oriented Programming.

Man man man ruby exception. don’t you want to continue the game or loop if an error occurred? Well, here is a way!

begin try to catch any error below
caught the error and display error and go to binding.pry

This is a lot of information to take in but as you can see the program is trying to dividing by zero. This cause an exception and rescue allow the program to keep running even if there is an exception. You can also store the exception by rescue => e and for bonus points do binding.pry to debug the program.

Inside the code/method/class you can make, modify, remove, and clear terminal things.

Pretty useful for doing things in the terminal while being in ruby.

Don’t you hate repetitive code? Don’t you want code that won’t likely to break other code? Don’t you want to fix a code in only one place instead of the entire freaking program? Well, modularize is your answer!

As you can see you can place a function outside and then use it inside. This allows it to be easier to read as well as you do not need to code one thing more than once.

And finally, Google. 80 percent of all things can be found in google because most likely someone else has the exact same problem that you have and solved it. So just take the answer and apply it into your code and see if that solve that problem. If it does not work then go to the next answer on the list until you find the answer.

binding.pry is your friend. Allow catching logic errors.

Modularize so you don’t have to code that code again.

You can use bash shell command through ‘system’.

The begin rescue catches the exception error and continues the program without exiting.

Overloading can change one inherence method to your liking

Google is also your friend. Best of all friends.

For more information here are some site to visit:

Add a comment

Related posts:

Perbedaan null vs undefined

Dalam pemrograman kalian pasti pernah menemui istilah null dan undefined. Tahukah kalian sebenarnya apa sih maksud dari null dan undefined tadi, serta apa sih perbedaan dan kesamaan dari keduanya…