Ruby introduction part1

520 views
458 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
520
On SlideShare
0
From Embeds
0
Number of Embeds
171
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ruby introduction part1

  1. 1. A Brief Introduction to Ruby : P1(Book Review of “Learn to Program”) Brady Cheng http://bradyisstudying.blogspot.tw/
  2. 2. AgendaInstallCommentData TypesVariables, Arrays, and IteratorsMethodsFlow Control
  3. 3. 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 
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. Variables, Arrays, and Iterators Arrays [ele1, ele2, ele3] Some array methods .pop .push .join(‘join string’) .last .length
  8. 8. 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
  9. 9. 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
  10. 10. MethodsSome fancy string methods .reverse .upcase .downcase .swapcase .capitalize .center (width) .ljust width .rjust width
  11. 11. MethodsSome number methods ** #exponential % #modular .abs #absolute value rand(number) #generate a random number srand(seed) #generate a random seed
  12. 12. 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
  13. 13. Flow ControlConditional flow controlLooping flow controlif condition1 while condition #do something #do somethingelsif condition2 end #do somethingelse #do somethingend Don’t forget end
  14. 14. The EndThe first part is over, please wait for the second partAny comments would be appreciatedWelcome to my study page http://bradyisstudying.blogspot.tw/

×