our concat example, but this time using the variable:
magicNumber = 3
puts magicNumber.to_s + " Is the magic number"
The output will be: 3 Is the magic number
In the above I have introduced a method on the Fixnum type. This method is called .to_s it turns the number into a string, which we can then use in our concat. Whenever you see the dot (.) in Ruby, it usually means that what is about to follow is a member of the class (in other words, either a property or a method of the type).
Now I have covered the very basics, I shall now cover the basics that every software developer needs.
- Selection -
- If example:
if x == 1
puts "Hello"
end
- Case example:
food = case shoppingItem
when "chips" then true
when "loo roll" then false
when "bananas" then true
when "guitar" then false
when "action figure" then false
end
- Iteration -
- A very useful while example:
# Centigrade to Fahrenheit converter
puts "Welcome to the C to F Converter\nPlease input the lowest value to convert\n"
low = gets().to_i
puts "Please input the highest value to convert\n"
high = gets().to_i
puts "Please input the interval to convert at"
interval = gets().to_i
currentTempC = low
currentTempF = 0
while currentTempC <= high
currentTempF = ((90/5)*currentTempC) + 320
puts currentTempC.to_s + "C = " + (currentTempF/10).to_s + "." + (currentTempF%10).to_s + "F \n"
currentTempC = currentTempC + interval
end
Here are some notes for the above:
# is a comment
gets() gets a value from keyboard
to_i turns a string into an integer
- A for loop example
for i in 1...20
puts "This is the loop count: " + i.to_s
end
- Class definitions
class Human
# variables and accessors go in here
# method definitions go in here
end
- Method definitions
def run
#put one foot in front of the next quickly, and repeat
end
This has been slow introduction, with a very quick run through of the basics of ruby. There are plenty of resources out there to learn about Ruby.
You can get Ruby and Ruby Documentation here: http://www.ruby-lang.org/
If you don't fancy downloading Ruby then you can use the online Interactive Ruby Shell available here: http://tryruby.hobix.com/
More information about Functional Ruby can be found here: http://www.artima.com/intv/clo sures.html
I can also provide training and consultancy for a small fee if you fancy learning more: http://www.vanirsystems.com/
Th is article is also available here: http://vanirsystems.com/daniel sblog/?p=120
Learn more about this author, Daniel Lewis.
Click here to send this author comments or questions.
Below are the top articles rated and ranked by Helium members on:
by Daniel Lewis
Ruby is a language, which, you will probably end up learning to love.
It is an Object-Oriented Language, which means that
Add your voice
Know something about The basics of Ruby?
We want to hear your view.
Write now!
Cast your vote!
Click for your side.
Featured Partner
Collegiate Society of America (CSAmerica)
The Collegiate Society of America (CSAmerica) has partnered with Helium, giving you the chance to write for a cause. ...more
hide