Updated June 28, 2023
Introduction to Rails Commands
Ruby on Rails is a web development framework written in Ruby Rails programming language, which is designed in such a way to make web application programming easier by making things a developer needs to start are installed before. In Ruby, we need to write a very less amount of code, whereas in other programming languages and frameworks. Web development using Ruby is also more fun. The main principles of Ruby programming language for web application development do not repeat yourself and convention over configuration. Ruby is a high-level programming language which is similar to interpreted languages such as Python, Perl, etc and object-oriented like Java, Ada, etc.
Basic Rails Commands
Following are the basic commands.
1. How to start a web server in Ruby Rails?
In Ruby, the web server will run virtually under any web server, but the best way to develop a web application using Rails is by using an in-built WEBrick server.
To start a web server we need to do the following steps:
- cd ruby/library
- Rails server
- and we need to open the browser and enter http://127.0.0.1:3000 on port 3000, and output will be as below:
2. How to set up database in Rails?
In Rails, we can set up different types of databases, and we will set up MySQL database, and we use the root as user id for the application, and we can perform different operations such as create a database, granting privileges etc. and sample output is as below:
3. How to create Active Record Files in Ruby?
In Ruby, we can create active record files for any project using a few commands of Rails. We will create active record files for library application with records as book and subject as below:
- rails script/generate model Book
- rails script/generate model subject
And it generates the output code as below:
4. How to create association/relations between models in Ruby?
In Rails command, we can create the association between models, and there are three types of associations such as one-to-one, one-to-many, many-to-many, and the sample code is as shown below. It creates a singular subject as one book belongs to one subject and output is:
5. How to create a migration file in Ruby?
In Rails, we can create a migration file using the below command, and it contains basic syntax which describes the data structure of the table
- rails generate migration table_name
- rails generate migration books
It will generate the below code as the output:
6. How to create string literals in Ruby?
In Ruby, strings are a sequence of characters which are represented in 8-bit and double-quoted strings allow substitution where single-quotes strings don’t, and sample code is as below:
puts 'escape using "\ \"';
puts 'That\'s right';
The output of the higher than code is as below:
7. How to declare an array in Rails?
In Rails, we can declare an array by having a combination of integers and strings separated by commas as below:
array = [ "fred", 10, 3.14, "This is a string", "last element", ]
array.each do |i|
puts i
end
And the output of the above code is as below:
8. What is Range and how to use it in Ruby?
In Ruby, a range is used to represent a set of values between the start and end of the range and constructed using for start and e for end literals and the sample code as below:
(10....14). each do |n|
print n, ' '
end
And the output of the above code is as below:
9. How to use a defined operator in Ruby?
In Ruby, defined? It is a special operator which takes a method call to determine whether the expression is passed or failed, and the sample code as below:
defined? Var — it validates to true if Var is initialized and examples as below:
10. How to use a single-line comment in Ruby?
In Ruby, comments can be represented in different ways such as single-line comment and multi-line comment and the sample code as below:
This is single line comment
puts "Hello, Ruby!"
And the output is as below:
Intermediate Rails Commands
Following are the intermediate commands.
1. How to generate a new application using the desired template in Ruby?
In Ruby, we can generate a new application using the below rails command new treehouse whereas we can use templates to run them against new application as below:
rails new treehouse –template=India
2. How to delete an element from an array at a particular index in Ruby?
In Ruby, we can delete an element from an array at a particular index using the below command:
array.delete_at(index)
Example
- array = [“hi”,”bar”,”foo”]
- array.delete_at(2)
- new array is : [“hi”,”bar”]
3. What is Interpolation and how to do in Ruby?
In Ruby, Interpolation is defined as combining a string with a variable or expression using double quotes is called Interpolation and the sample code is as below:
"Email : # {user.email}"
" A string and an #{expression}"
4. How to call a method in Ruby?
In Ruby, we can call a method using the object and calling a method is like a sending a message in Ruby as we are sending an object with some message and waiting for its response and the sample code as below:
Example:
- object.method(arguments)
- string.length
- array.delet
5. How to create and use hashes in Ruby?
In Ruby, hashes can be created by having key-value pairs in curly braces and the key will always point to the value by an arrow and sample code is as below:
{42 => "answer" , "score" =>100, :name=> "Das" }
Advanced Rails Commands
Following are the advanced commands.
1. What is collect iterator and how to use it in Ruby?
In Ruby, collect iterator is used to return all elements of a collection and collect method no need to be associated with a block and sample code as below:
collection = collection.collect
a=[1,2,3]
b=Array.new
b=a.collect
puts b
2. Write a web service handler method in Ruby?
In Ruby, Web services methods can be written as simple Ruby Rails methods which will expose to the outside world and sample code two perform addition of two numbers as below in command:
class MyServer < SOAP:: RPC:: StandaloneServer
#handler methods
def add(a,b)
return a+b
end
end
Tips and Tricks to Use
- Use extract regular expression which matches quickly
- Best way to join the strings as Array#join e,g: Array#*
- Use format decimal as it amounts quickly
- Perform Interpolation as it interpolates text quickly
Conclusion
Finally, Its an overview of Ruby on Rails commands of different types. I hope you will be having good knowledge about Ruby on Rails commands after reading this article.
Recommended Articles
We hope that this EDUCBA information on “Rails Commands” was beneficial to you. You can view EDUCBA’s recommended articles for more information.