Ruby introduction part1
Upcoming SlideShare
Loading in...5
×
 

Ruby introduction part1

on

  • 482 views

 

Statistics

Views

Total Views
482
Views on SlideShare
313
Embed Views
169

Actions

Likes
0
Downloads
0
Comments
0

4 Embeds 169

http://bradyisstudying.blogspot.tw 157
http://bradyisstudying.blogspot.com 10
http://www.hanrss.com 1
http://bradyisstudying.blogspot.kr 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Ruby introduction part1 Ruby introduction part1 Presentation Transcript

  • A Brief Introduction to Ruby : P1(Book Review of “Learn to Program”) Brady Cheng http://bradyisstudying.blogspot.tw/
  • AgendaInstallCommentData TypesVariables, Arrays, and IteratorsMethodsFlow Control
  • InstallFor Windows http://rubyinstaller.org/For Mac Build-inI strongly recommend you to use Mac or Linux to develop Ruby programs. But in this tutorial, I am gonna use Windows since I am stiff-necked guy 
  • CommentComment in Ruby is similar to Perl Ruby Perl # comment a line # comment a line =begin =pod Comment a block Comment a block =end =cut
  • Data Types Numbers puts 1+2 #3 Integer print puts 2*3 #6 puts 5-8 #-3 Float puts 9/2 #4 Letters puts 9.to_f/2 #4.5 String Cast to float -------------------------- puts ‘Enter a string’ #Enter a string More: puts gets.chomp #input from keyboard .to_i puts ha*3 #hahaha .to_s puts=<STDOUT>Repeat 3 times gets=<STDIN> chomp=delete last char
  • Variables, Arrays, and Iterators In C/C++, we write int a = 1; in Perl, we write $a = 1; in Ruby… a= 1 Yes, just write a a= 1 b = ‘2’ c = a.to_s + b #12 Cast a to string Add(+) = conj strings
  • Variables, Arrays, and Iterators Arrays [ele1, ele2, ele3] Some array methods .pop .push .join(‘join string’) .last .length
  • Variables, Arrays, and Iterators There is a special array method : each iterator An array with 3 elements color= [‘R, G, R] color.each do | c | | c | = variable in this puts “color=” + c do…end block end Output= color=R color=G color=B
  • MethodsIf objects(variables, arrays, …) are nouns inEnglish, then methods are verbs! int=noun We say “int, convert to string” to_s=verb int.to_s Sometimes we just say a verb without any noun, ie. GO! gets User defined methods
  • MethodsSome fancy string methods .reverse .upcase .downcase .swapcase .capitalize .center (width) .ljust width .rjust width
  • MethodsSome number methods ** #exponential % #modular .abs #absolute value rand(number) #generate a random number srand(seed) #generate a random seed
  • MethodsMore on user defined methods, similar to Python!def functionName (arg1, arg2) #do something return ret_valend Actually we don’t have to return because…. Case1: the function doesn’t have to return Case2: the last line is the default return value
  • Flow ControlConditional flow controlLooping flow controlif condition1 while condition #do something #do somethingelsif condition2 end #do somethingelse #do somethingend Don’t forget end
  • The EndThe first part is over, please wait for the second partAny comments would be appreciatedWelcome to my study page http://bradyisstudying.blogspot.tw/