Ruby Crash-Course
So, you want to dive into your first Rails application! But Rails is only a framework–the underlying programming language is still Ruby. This crash course teaches you the basics of Ruby, enough to get your first Rails application rocketing to success! (We won’t cover anything advanced–only basic stuff. For more advanced stuff, see our other articles.)
First, know that Ruby is an object-oriented programming language. Everything is an object, and every object has functions. (Rails is a framework that provides you with some basic classes and functions to make your web-development life easier. More on that in our Rails crash-course.)
Ruby variables are weakly-typed–you just assign values, you never declare types. eg.
i = 20 # a numeric value
myName = "George" # a string
todo = ["Clean garage", "Go shopping for clothes", "Finish email to boss"] # an array
personDetails = {"name" => "Bob Smith", "address" => "3829 Cherry Crescent"} # a hash-table
Comments begin with the # symbol. Symbols–reusable strings–begin with a colon, e.g. :name, :address
Since code explains itself, I’ll just list some common tasks and the code you write you perform that task.
- To output a value from inside a view file:
<%= someValueHere %> - To output multiple values in a string, use #{value}’, like so:
"User #{user.name.capitalize}; joined on #{user.joinDate} - To convert a value into an integer:
value.to_i - To convert a value into a string:
value.to_s - For-loops:
listVariable.each do |item|
item.someFunction
end
- While-loops:
while condition
someVar.doSomeStuff
end
- If-Statements:
if user.role == "Admin"
curPage.printAdminLinks
elsif user.role == "Contributer"
curPage.showEditLinks
else
curPage.incrementViews
end
- To output something to the server console window:
puts "debug: blah" - To create a new instance of a class:
instance = ClassName.new - Arrays are a class, too. The main functions you need are:
- To access the element at index i:
arrayName[i] - To append something to the end of the array:
arrayName.push(newObject) - To remove the last object:
arrayName.pop() - To see (without removing) the first object:
arrayName.first - To see (without removing) the last object:
arrayName.last
- To access the element at index i:
And that should be it! With only these functions in mind, you should be able to understand most beginner tutorials, as well as build your own small web application.
Did we miss something crucial? If so, add it in a comment!
Tags: introductory, ruby Posted in


Leave a Reply