Ruby 入門 第一次就上手

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    3 Favorites & 1 Group

    Ruby 入門 第一次就上手 - Presentation Transcript

    1. Ruby ihower@gmail.com http://creativecommons.org/licenses/by-nc/2.5/tw/
    2. ? • a.k.a. ihower • http://ihower.idv.tw/blog/ • http://twitter.com/ihower • 2006 Ruby • ( ) Rails Developer • http://handlino.com • http://registrano.com
    3. ? • a.k.a Ryudo • http://www.freebbs.tw • http://www.gameckub.tw • 2007 Rails • TOMLAN FOUNDER
    4. Ruby? • (interpreted) • • • Yukihiro Matsumoto, a.k.a. Matz • Lisp, Perl, Smalltalk • Happy
    5. • Ruby 1.8.6 One-click Installer • http://www.ruby-lang.org/en/downloads/ • Next -> Agree -> Enable Rubygems
    6. Part1:
    7. irb: Interactive Ruby irb(main):001:0> irb(main):001:0> 1 + 1 => 2 irb(main):002:0> 100 * 5 + 99
    8. PUTS • notepad++ hello.rb puts "Hello World!" • ruby number.rb
    9. Integer 5 -205 9999999999 0
    10. Float . 54.321 0.001 -12.312 0.0
    11. puts 1.0 + 2.0 puts 2.0 * 3.0 puts 5.0 - 8.0 puts 9.0 / 2.0 # 3.0 # 6.0 # -3.0 # 4.5
    12. puts 1 + 2 puts 2 * 3 puts 5 - 8 puts 9 / 2 # 3 # 6 # -1 # 4
    13. puts 5 * (12-8) + -15 puts 98 + (59872 / (13*8)) * -52
    14. String puts 'Hello, world!' puts '' puts 'Good-bye.'
    15. puts 'I like ' + 'apple pie.' puts 'You're smart!' puts '12' + 12 #<TypeError: can't convert Fixnum into String>
    16. Variable composer = 'Mozart' puts composer + ' was "da bomb", in his day.' composer = 'Beethoven' puts 'But I prefer ' + composer + ', personally.'
    17. Conversions var1 = 2 var2 = '5' puts var1.to_s + var2 # 25 puts var1 + var2.to_i # 7 puts 9.to_f / 2 # 4.5
    18. GETS puts 'Hello there, and what's your name?' name = gets.chomp puts 'Your name is ' + name + '? What a lovely name!' puts 'Pleased to meet you, ' + name + '. :)'
    19. Methods • (Object) • . • self puts self.puts
    20. var1 = 'stop' var2 = 'foobar' var3 = "aAbBcC" puts var1.reverse # 'pots' puts var2.length # 6 puts var3.upcase puts var3.downcase
    21. Flow Control
    22. puts 1 > 2 puts 1 < 2 puts 5 >= 5 puts 5 <= 4 puts 1 == 1 puts 2 != 1 puts ( 2 > 1 ) && ( 2 > 3 ) # and puts ( 2 > 1 ) || ( 2 > 3 ) # or
    23. false nil puts "not execute" if nil puts "not execute" if false puts "execute" if true # execute puts "execute" if “” # execute ( JavaScript ) puts "execute" if 0 # execute ( C ) puts "execute" if 1 # execute puts "execute" if "foo" # execute puts "execute" if Array.new # execute
    24. puts "hello, who are you?" name = gets.chomp if name == "you" puts "that's you" elsif name == "me" puts "that's me" else puts "no ideas" end
    25. Case case name when "John" puts "Howdy John!" when "Ryan" puts "Whatz up Ryan!" else puts "Hi #{name}!" end
    26. command = '' while command != 'bye' puts command command = gets.chomp end puts 'Come again soon!'
    27. Array a = [ 1, "cat", 3.14 ] puts a[0] # 1 puts a[1] # “cat” puts a.size # 3
    28. colors = ["red", "blue"] colors.push("black") colors << "white" puts colors.join(", ") # red, blue, black, white colors.pop puts colors.last #black
    29. each method languages = ['Ruby', 'Javascript', 'Perl'] languages.each do |lang| puts 'I love ' + lang + '!' end # I Love Ruby # I Love Javascript # I Love Perl
    30. iterator • while each (iterator) • do .... end each ( code block)
    31. 3.times do puts 'Good Job!' end # Good Job! # Good Job! # Good Job!
    32. code block closure { puts "Hello" } # block do puts "Blah" # block puts "Blah" end
    33. code block # a = [ "a", "b", "c", "d" ] b = a.map {|x| x + "!" } puts b.inspect # ["a!", "b!", "c!", "d!"] # b = [1,2,3].find_all{ |x| x % 2 == 0 } b.inspect # [2]
    34. code block file = File.new("testfile", "r") # ... file.close File.open("testfile", "r") do |file| # ... end #
    35. Hash (Associative Array) config = { "foo" => 123, "bar" => 456 } puts config["foo"] # 123
    36. Symbols config = { :foo => 123, :bar => 456 } puts config[:foo] # 123
    37. Methods def end def say_hello(name) result = "Hi, " + name return result end puts say_hello('ihower') # Hi, ihower
    38. Methods def end def say_hello(name) result = "Hi, " + name return result end puts say_hello('ihower') # Hi, ihower
    39. Person Class Attributes Methods john = Person.new mary = Person.new Object Object Attributes Methods Attributes Methods
    40. Classes new color_string = String.new color_string = "" # color_array = Array.new color_array = [] # color_hash = Hash.new color_hash = {} # time = Time.new puts time
    41. class Greeter def initialize(name) @name = name end def say(word) puts "#{word}, #{@name}" end end g1 = Greeter.new("ihower") g2 = Greeter.new("ihover") g1.say("Hello") # Hello, ihower g2.say("Hello") # Hello, ihover
    42. class Greeter def initialize(name) @name = name end def say(word) puts "#{word}, #{@name}" end end g1 = Greeter.new("ihower") g2 = Greeter.new("ihover") g1.say("Hello") # Hello, ihower g2.say("Hello") # Hello, ihover
    43. class Greeter def initialize(name) @name = name end def say(word) puts "#{word}, #{@name}" end end g1 = Greeter.new("ihower") g2 = Greeter.new("ihover") g1.say("Hello") # Hello, ihower g2.say("Hello") # Hello, ihover
    44. class Greeter def initialize(name) @name = name end ( ) def say(word) puts "#{word}, #{@name}" end end g1 = Greeter.new("ihower") g2 = Greeter.new("ihover") g1.say("Hello") # Hello, ihower g2.say("Hello") # Hello, ihover
    45. class Greeter @@name = “ihower” def self.say puts @@name end end Greeter.say # Hello, ihower
    46. class Greeter @@name = “ihower” def self.say puts @@name end end Greeter.say # Hello, ihower
    47. class Greeter @@name = “ihower” def self.say puts @@name end end Greeter.say # Hello, ihower
    48. attr_accessor, attr_writer, attr_reader class Person class Person def name @name attr_accessor :name end end def name=(val) @name = val end end
    49. Part2: gem install sinatra -y --no-ri --no-rdoc gem install nokogiri -y --no-ri --no-rdoc
    50. RubyGems • Ruby • gem list • http://gemcutter.org/
    51. Sinatra • web development • http://www.sinatrarb.com/ • gem install sinatra
    52. web app ruby myapp.rb http://localhost:4567 # myapp.rb require 'rubygems' require 'sinatra' get '/' do 'Hello world!' end
    53. URL require 'rubygems' require 'sinatra' get '/hello/:name' do # "GET /hello/foo" "GET /hello/bar" # params[:name] 'foo' 'bar' "Hello #{params[:name]}!" end
    54. template views hello.erb # myapp.rb require 'rubygems' require 'sinatra' get '/hello/:name' do erb :hello end
    55. ERB (Ruby Template) <!DOCTYPE html> <html> <head> <title>Sinatra app</title> </head> <body> <% 10.times do %> <p><%= "Hello #{params[:name]}!" %></p> <% end %> </body> </html>
    56. Template # myapp.rb require 'rubygems' require 'sinatra' get '/array' do @arr = ["aaa","bbb","ccc","ddd"] erb :array end # views/array.erb <% @arr.each do |item| %> <p><%= item %></p> <% end %>
    57. # myapp.rb # views/index.erb require 'rubygems' <form action="/query" method="post"> require 'sinatra' <p><input type="text" name="keyword" value=""></p> get '/' do <p><input type="submit" value="Submit"></p> erb :index end </form> post '/query' do params[:keyword] end
    58. nokogiri • HTML, XML, SAX (parser) • XPath CSS • gem install nokogiri
    59. require 'rubygems' require 'nokogiri' text = "<html> <ul> <li>FOOOBARRR</li> <li>AAAAACCCCC</li> <ul> </html>" doc = Nokogiri::HTML(text) puts doc.css('li').size # 2 doc.css('li').each do |item| puts item.content end # FOOOBARRR # AAAAACCCCC
    60. sinatra+nokogiri # myapp.rb require 'rubygems' require 'sinatra' require 'nokogiri' require 'open-uri' get '/' do erb :index end post '/query' do html = open( params[:keyword] ).read doc = Nokogiri::HTML(html) @links = doc.css('a') erb :query end
    61. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <ul> <% @links.each do |link| %> <li> <%= link.content %> <%= link.attributes["href"] %> </li> <% end %> </ul> </body>
    62. Thank you. Learn to Program http://pine.fm/LearnToProgram/ Beginning Ruby 2nd. (Apress)

    + Wen-Tien ChangWen-Tien Chang, 2 weeks ago

    custom

    665 views, 3 favs, 3 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 665
      • 497 on SlideShare
      • 168 from embeds
    • Comments 0
    • Favorites 3
    • Downloads 30
    Most viewed embeds
    • 102 views on http://ihower.idv.tw
    • 65 views on http://ihower.tw
    • 1 views on http://dir.asikart.com

    more

    All embeds
    • 102 views on http://ihower.idv.tw
    • 65 views on http://ihower.tw
    • 1 views on http://dir.asikart.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Groups / Events