Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
Ruby Tutorial for Beginners

Ruby Tutorial for Beginners

Ratings: (0)|Views: 242|Likes:
Published by Abhilash V Pillai
Simple and detail Ruby porgramming tutorial for beginners, with number of examples for each section.
Simple and detail Ruby porgramming tutorial for beginners, with number of examples for each section.

More info:

Published by: Abhilash V Pillai on Feb 22, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

11/06/2013

pdf

text

original

 
Ruby for the Attention Deficit Disorder Programmer - let's start learning Ruby fast!
"Java is a pair of scissors, Ruby is a chainsaw."1.Install RubyFor windows you can download Ruby fromhttp://rubyforge.org/frs/?group_id=167for Linux tryhttp://www.rpmfind.net.2.Our first programEnter the following into the file, "test.rb".
 puts "Howdy!"
At the C: prompt enter,
C:>ruby test.rb
This produces:
 Howdy!
OK, daylight's burning, let's move on.3.Output in Ruby"puts" writes to the screen with a carriage return at the end."print" does the same thing without the carriage return."printf" formats variables like in C and Java 5.
 puts "puts works"puts " with line breaks."print "print works"print " with no line breaks."printf("\n\nprintf formats numbers like %7.2f, andstrings like %s.",3.14156,"me")
This produces:
puts workswith line breaks.print works with no line breaks.printf formats numbers like 3.14, and strings likeme.
4.Reading from the ConsoleUse "gets"
 
puts "What is your name?"$name = STDIN.getsputs "Hi "+$name
5.Functions1.Our first Ruby function'def' starts the definition of a method, and 'end' ends it - no cute little curly braces.
def welcome(name)puts "howdy #{name}" # inside double quotes, #{ } willevaluate the variableendwelcome("nana") # traditional parens
This Produces:
howdy nana
2.Parentheses are optional
3.def welcome(name)4. puts "howdy #{name}" # inside double quotes, #{ } willevaluate the variable5.end6.welcome "visitor" #look, ma, no parentheses
This Produces:
howdy visitor
The same is true of methods without arguments
"hello".upcase() => "HELLO""hello".upcase => "HELLO"
7.How to return values from a functionWe can use the faithful 'return'
def multiply(a,b)product = a * breturn productendputs multiply(2,3) =>6
Oddly enough you can leave out the "return" statement, and Ruby will helpfullyreturn the last expression:
def mult(a,b)product = a * bendputs mult(2,3)
or even simpler, leave out "product" and ruby returns the contents of the lastexpression:
def mult(a,b)a * b
 
endputs mult(3,3) =>9
8.Optional argument valuesRuby lets you assign values to arguments which may, or may not be supplied asshown below:
def test(a=1,b=2,c=a+b)puts "#{a},#{b},#{c}"endtest => 1,2,3test 5 => 5,2,7test 4, 6 => 4,6,10test 3, 4, 6 => 3,4,6
9.Extra argumentsExtra arguments are gathered into the last variable if preceded with a "*". ("each"is an iterator that loops over its members).
def test(a=1,b=2,*c)puts "#{a},#{b}"c.each{|x| print " #{x}, "} # We will learn about "each" verysoon. I promise.endtest 3, 6, 9, 12, 15, 18
This produces:
3,69, 12, 15, 18,
10.Multiple return values
11.def getCostAndMpg12. cost = 30000 # some fancy db calls go here13. mpg = 3014. return cost,mpg15.end16.AltimaCost, AltimaMpg = getCostAndMpg17.puts "AltimaCost = #{AltimaCost}, AltimaMpg = #{AltimaMpg}"
Produces:
AltimaCost = 30000, AltimaMpg = 30
6.Open ClassesYou can add methods to existing library classes. For example, in C# 2.0, Microsoft addedthe very helpful string function, IsNullOrEmpty() which replaces the unwieldly construct:if(mystring != null && mystring != "")In Ruby you don't have to wait for the mavens in Redmond to decide you need a newstring function, you can add it yourself.
class Stringdef NullOrEmpty?(self == nil || self == "")end

Activity (2)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->