Ruby is one of the most versatile programming languages that supports several built-in functions to interact and execute desired functions on the operating system. It is widely used for creating web applications and support for multiple programming paradigms. Besides that, it has an elegant syntax, which you can easily read and understood.
If you want to learn Ruby on a Raspberry Pi system, follow this article to install this programming language and use it to code on your system.
Install and Use Ruby on Raspberry Pi
By default, Ruby is installed by default on Raspberry Pi system, however, if you have accidentally deleted it, use the following command anytime to install it on Raspberry Pi:
In order to check the version of the installed Ruby language on Raspberry Pi, type the following command:
Use Ruby on Raspberry Pi
To start learning Ruby programming language, first it’s better to create a projects directory using the following command:
After that, you must navigate to the projects directory using the following command:
The file extension for Ruby is .rb, so, any file you create must have a .rb extension. For the time being, I am creating a project file with the name “hello.rb” that outputs the message on the terminal.
To create a file, you can use the default “nano” editor.
Add the following simple code to output the “hello” message:
puts(message)
Save the file using “CTRL+X” keys and add Y, then press Enter to exit.
To run the file using Ruby, apply the following code.
In this way, you can write and run multiple Ruby codes on the Raspberry Pi system through this way.
Create a Web Application Using Ruby
You can also use the “Sinatra” service to quickly create a web application with Ruby. However, before that, you must install “Sinatra” from the following command:
Afterwards, create a new file using the nano editor with .rb extension.
Then add the following lines inside the file:
get '/hi' do
"Message"
end
Save the file.
Now, use the command given below to run the file:
Now, open the Raspberry Pi browser and enter the following address to view the message on the web.
Through this way, you can create different web applications using Sinatra and Ruby.
Conclusion
Ruby is an open-source programming language used for creating web applications. Though it’s already installed on the Raspberry Pi system, you can follow the above guidelines to learn how to use this language to print a simple message or utilize Sinatra with Ruby to quickly create a web application on Raspberry Pi system.