Ruby/Variables Datatypes Operators3

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application
Strings.Page 2

Every time you want to assign a string to a variable, remember to use the quotes. Let's try imagining we're trying to write a very similar application to keep tracking of books you borrow from your friends and need to give back.

So let's assume Bob gave you his copy of "Fight Club" and told you that you'd have to give it back in 2 weeks and Mark gave you his C++ programming for beginners textbook for 3 week. You'd want to check the application from time to time to see the day you got each book and how much time is left till you need to give them back.We will assume both books have been borrowed during the same month and will also be returned the same month for the simplicity of the code.

Most of the code would be this:

firstname="Bob"
secondname="Mark"
 
bob_start=10
mark_start=5
 
bob_return=24
mark_return=26
 
puts "#{firstname} gave me the book on #{bob_start} this month.I have #{bob_return-bob_start} days left"

This could've also been written like this:

puts "#{firstname} gave me the book on #{bob_start} this month"+" "+"I have #{bob_return-bob_start} days left"

We use the "+" operator to concatenate the 2 strings, with one space between them.


Image:Strings.gif

You also now learned how to display values of variables inside printed screen messages by using the # sign before a variable enclosed in { }.


The difference between single-quoted enclosed strings and double quoted enclosed strings

Enclosing a string in single quotes means that only 2 escape sequences will be recognized if included inside the string: the escaped backslash (\\) and the escaped (\') single quote.

If you don't know what escape sequences are used for: if you want a certain character besides a letter or number to correctly display inside a quoted string, you must "escape" it with a backslash first, otherwise it mightbe interpreted as something else that what you intended.

Since we are talking about single quoted strings now, think of this:

x='this is a single quoted string'
y='this is John's single quoted string'
z='you can find the ebook in d:\\ebooks'

Image:Strings_example.gif

Previous Next
Personal tools