Ruby: A Simple Program

Lewis Coldwell
The Yorkshire Coder
5 min readFeb 21, 2018

In this post I will show you how to build a simple program in Ruby that will allow us to give feedback on a students grades. There are multiple ways you can write code in Ruby but what is neater and pleasing to the eye will always be the preferred method.

Firstly, do you have a code editor? We will need one installed to edit Ruby code. The popular, industry leading, editor at the moment is VScode which can be downloaded and installed by visiting https://code.visualstudio.com/.

Once you have this installed, open it and then open up your chosen terminal. We now need to create our Ruby file for us to edit. To create a new file you should open your chosen terminal and navigate to the directory you want to create your file/folder in, which in my case will be in the desktop (I recommend you stay close to your home directory):

$cd /desktopThis will navigate us to the desktop$mkdir testThis will create a new directory named 'test'$cd /test$touch app.rbThe last command creates a ruby file in the 'test' directory.

It is important to name your file *.rb’ so terminal recognises it as a Ruby file later when we execute.

Once you have done this, it is time to open up your file in VScode. If you don’t have command line tools enabled (you can enable these by visiting: https://code.visualstudio.com/docs/setup/mac) you should switch back to VScode and navigate to your newly created Ruby file from the navigation pane. Otherwise you can execute it from the command line using:

code app.rbAssuming you are still in the '/test' directory we just created; otherwise you will need to insert the correct path

Now we’re ready to start coding! (Don’t forget to keep you terminal open and in the path your ‘*.rb’ file is located.)

We first need to define our constants, which in this will be the grade boundaries. For the sake of this exercise I have defined them myself, but you may wish to use your own values. Enter it into VScode:

PASS_MARK = 45CREDIT_MARK = 65DISTINCTION_MARK = 85

Make sure it is in capitals to emphasise this value is constant, if you believe these boundaries would change you would put it into lowercase. Once you have defined these boundaries, lets proceed to building our program. We want to get the user to be greeted with a prompt for their mark. The question is delivered using the ‘puts’ command which PUTS a string out to the user and then we request an input using the ‘gets’ prompt. See below and try for yourself:

puts "Hello, to continue please input your mark out of 100"grade = gets

We now have a system in place that will allow the user to respond but no response. Now the ‘gets’ command itself will give us a string value when we need an integer response. This is resolved by inputting the following command:

grade = grade.to_i

Once you have done this we are ready to give the user a response:

puts "Your grade is #{grade}"

We can now run this in terminal by typing the command:

ruby app.rb

Did it work? If it did, you should have got a response when you insert your “grade”.

Next, we would like to give the user some feedback. We can do this by introducing the ‘case’ command that can analyse a certain string/variable and produce a result in the end. In this case we want the program to work through possible scenarios and deliver a “message” to the user. The ‘case’ command works just like ‘if’ and ‘elsif’ in that we want a “message” to be displayed that matches our rules. To do this we use the ‘which’ condition based on our constants that we defined at the beginning:

message = casewhen grade >= DISTINCTION_MARK then"You achieved a distinction, congratulations!"when grade >= CREDIT_MARK then"You received a credit, well done!"when grade >= PASS_MARK then"You passed!"else"You failed, you will need to resit."endputs message

The order of these commands is very important. If you include the lower grade boundary at the top — the program will ignore the other conditions as any mark above or equal to the PASS_MARK will deliver the response for PASS_MARK. So ensure you always keep it in a logical order.

Now if you go back to your terminal and run the program again you will find some feedback for you grade!

We can modify this and add feedback depending on if the student failed or not. So for starters remove that last ‘puts message’ and input this new snazzy bit of code, don’t worry — i’ll break it down:

message += "\n"message += grade <= 45? 'Please contact the school office' : 'Good luck next year!'puts message
Your final VScode should look a bit like this

So the first line simply states that we want to add a new line (\n) to the message generated in our previous ‘case’ command. The second line acts as a “if” command. In layman terms it says: Is the grade less than 45? If so contact the school. If its not (:) then good luck! We then finish off with the original ‘puts message’ we omitted and this should now give us additional feedback depending on the students circumstances.

Run your program for one last time! There you have it, a simple, yet effective program to deliver feedback for a students test results.

In the next posts we will begin to work with more complex ruby commands.

--

--

Lewis Coldwell
The Yorkshire Coder

Currently studying Full Stack Development at the Coder Academy, Sydney in their bootcamp program.