Contenu connexe Similaire à Ruby 入門 第一次就上手 Similaire à Ruby 入門 第一次就上手 (20) Plus de Wen-Tien Chang (20) Ruby 入門 第一次就上手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
8. PUTS
• notepad++ hello.rb
puts "Hello World!"
• ruby number.rb
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
15. puts 'I like ' + 'apple pie.'
puts 'You're smart!'
puts '12' + 12
#<TypeError: can't convert Fixnum into String>
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
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
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
49. Part2:
gem install sinatra -y --no-ri --no-rdoc
gem install nokogiri -y --no-ri --no-rdoc
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
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>
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
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>