SlideShare ist ein Scribd-Unternehmen logo
1 von 5
Downloaden Sie, um offline zu lesen
Index by each word
   A quick Ruby Tutorial, Part 3
                                                          class WordIndex
                                                           def initialize
                                                            @index = {}
                                                           end
                                                           def add_to_index(obj, *phrases)
                                                             phrases.each do |phrase|
                                                              phrase.scan(/w[-w']+/) do |word| # extract each word
                                                               word.downcase!
               COMP313                                         @index[word] = [] if @index[word].nil?
                                                               @index[word].push(obj)
Source: Programming Ruby, The Pragmatic                        end
                                                              end
Programmers’ Guide by Dave Thomas, Chad                     end
                                                           def lookup(word)
          Fowler, and Andy Hunt                              @index[word.downcase]
                                                           end
                                                          end




        Add full index to SongList                                                          Ranges
 class SongList                                           1..10
  def initialize                                          'a'..'z'
    @songs = Array.new                                    my_array = [ 1, 2, 3 ]
    @index = WordIndex.new                                0...my_array.length
  end
  def append(song)                                        (1..10).to_a       [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    @songs.push(song)                                     ('bar'..'bat').to_a     ["bar", "bas", "bat"]
    @index.add_to_index(song, song.name, song.artist)
    self                                                  digits = 0..9
  end                                                     digits.include?(5)        true
  def lookup(word)                                        digits.min      0
    @index.lookup(word)                                   digits.max       9
  end                                                     digits.reject {|i| i < 5 }     [5, 6, 7, 8, 9]
 end                                                      digits.each {|digit| dial(digit) }      0..9




      Range example: VU meter
 class VU
                                                             VU Ranges / Ranges in conditions
  include Comparable
  attr :volume
  def initialize(volume) # 0..9                           medium_volume = VU.new(4)..VU.new(7)
    @volume = volume                                      medium_volume.to_a    [####, #####, ######, #######]
  end
                                                          medium_volume.include?(VU.new(3))    false
  def inspect
    '#' * @volume
  end                                                     (1..10) === 5      true
  # Support for ranges
                                                          (1..10) === 15       false
  def <=>(other)
    self.volume <=> other.volume                          (1..10) === 3.14159        true
  end                                                     ('a'..'j') === 'c'  true
  def succ
    raise(IndexError, "Volume too big") if @volume >= 9
                                                          ('a'..'j') === 'z'  false
    VU.new(@volume.succ)
  end
 end




                                                                                                                       1
Regular expressions                                                  Special vars $` $& $’
a = Regexp.new('^s*[a-z]')       /^s*[a-z]/                          def show_regexp(a, re)
                                                                        if a =~ re
b = /^s*[a-z]/   /^s*[a-z]/
                                                                          "#{$`}<<#{$&}>>#{$'}"
c = %r{^s*[a-z]}     /^s*[a-z]/                                       else
                                                                          "no match"
                                                                        end
                                                                       end
name = "Fats Waller"
name =~ /a/   1                                                        show_regexp('very interesting', /t/)    very in<<t>>eresting
                                                                       show_regexp('Fats Waller', /a/)      F<<a>>ts Waller
name =~ /z/   nil
                                                                       show_regexp('Fats Waller', /ll/)     Fats Wa<<ll>>er
/a/ =~ name   1                                                        show_regexp('Fats Waller', /z/)      no match




          Special chars, anchors                                                          More RegExps
., |, (, ), [, ], {, }, +, , ^, $, *, and ?                           show_regexp('Price $12.', /[aeiou]/)      Pr<<i>>ce $12.
                                                                       show_regexp('Price $12.', /[s]/)     Price<< >>$12.
show_regexp('kangaroo', /angar/)               k<<angar>>oo            show_regexp('Price $12.', /[[:digit:]]/)   Price $<<1>>2.
                                                                       show_regexp('Price $12.', /[[:space:]]/)     Price<< >>$12.
show_regexp('!@%&-_=+', /%&/)                  !@<<%&>>-_=+
                                                                       show_regexp('Price $12.', /[[:punct:]aeiou]/)     Pr<<i>>ce $12.

show_regexp("this isnthe time", /^the/)     this isn<<the>> time
                                                                       a = 'see [Design Patterns-page 123]'
show_regexp("this isnthe time", /is$/)    this <<is>>nthe time       show_regexp(a, /[A-F]/)     see [<<D>>esign Patterns-page 123]
show_regexp("this isnthe time", /Athis/)     <<this>> isnthe time   show_regexp(a, /[A-Fa-f]/)    s<<e>>e [Design Patterns-page 123]
 show_regexp("this isnthe time", /Athe/) no match                    show_regexp(a, /[0-9]/)    see [Design Patterns-page <<1>>23]
                                                                       show_regexp(a, /[0-9][0-9]/)   see [Design Patterns-page <<12>>3]




                  character classes                                                             Repetition
d [0-9] Digit character                                               r* matches zero or more occurrences of r.
D [^0-9] Any character except a digit                                 r+ matches one or more occurrences of r.
s [ trnf] Whitespace character                                    r? matches zero or one occurrence of r.
S [^ trnf] Any character except whitespace                        r{m,n} matches at least m and at most n occurrences of r.
w [A-Za-z0-9_] Word character                                         r{m,} matches at least m occurrences of r.
W [^A-Za-z0-9_] Any character except a word character                 r{m} matches exactly m occurrences of r.

                                                                       /ab+/     matches ab, abb, abbbb, …
                                                                       /(ab)+/   matches ab, abab, ababab, …
                                                                       /a*/      matches everything (why?)




                                                                                                                                           2
Greedy matching/Alternatives                                                              Groupings
a = "The moon is made of cheese"                                   show_regexp('banana', /an*/)   b<<an>>ana
show_regexp(a, /w+/)    <<The>> moon is made of cheese            show_regexp('banana', /(an)*/)  <<>>banana
show_regexp(a, /s.*s/)  The<< moon is made of >>cheese           show_regexp('banana', /(an)+/)   b<<anan>>a

show_regexp(a, /s.*?s/)   The<< moon >>is made of cheese
                                                                   a = 'red ball blue sky'
show_regexp(a, /[aeiou]{2,99}/)  The m<<oo>>n is made of
                                                                   show_regexp(a, /blue|red/)    <<red>> ball blue sky
   cheese
                                                                   show_regexp(a, /(blue|red) w+/)    <<red ball>> blue sky
                                                                   show_regexp(a, /(red|blue) w+/)    <<red ball>> blue sky
a = "red ball blue sky"
                                                                   show_regexp(a, /red|blue w+/)    <<red>> ball blue sky
show_regexp(a, /d|e/)        r<<e>>d ball blue sky                 show_regexp(a, /red (ball|angry) sky/)   no match a = 'the red angry sky'
show_regexp(a, /al|lu/)   red b<<al>>l blue sky                    show_regexp(a, /red (ball|angry) sky/)   the <<red angry sky>>
show_regexp(a, /red ball|angry sky/)   <<red ball>> blue sky




     Groupings collect matches                                              Match inside with 1,…
"12:50am" =~ /(dd):(dd)(..)/   0                               # match duplicated letter
"Hour is #$1, minute #$2"       "Hour is 12, minute 50"            show_regexp('He said "Hello"', /(w)1/)        He said "He<<ll>>o"
                                                                   # match duplicated substrings
"12:50am" =~ /((dd):(dd))(..)/ 0                               show_regexp('Mississippi', /(w+)1/)       M<<ississ>>ippi
"Time is #$1"    "Time is 12:50"
"Hour is #$2, minute #$3"
"Hour is 12, minute 50"                                            show_regexp('He said "Hello"', /(["']).*?1/)     He said <<"Hello">>
"AM/PM is #$4"     "AM/PM is am"                                   show_regexp("He said 'Hello'", /(["']).*?1/)     He said <<'Hello'>>




              Substitute patterns                                           Upcase every first char
a = "the quick brown fox"                                          def mixed_case(name)
a.sub(/[aeiou]/, '*')     "th* quick brown fox"                     name.gsub(/bw/) {|word| word.upcase }
a.gsub(/[aeiou]/, '*')      "th* q**ck br*wn f*x"                  end
a.sub(/sS+/, '')     "the brown fox"
a.gsub(/sS+/, '')     "the"                                      mixed_case("fats waller")  "Fats Waller"
                                                                   mixed_case("louis armstrong")   "Louis Armstrong"
a.sub(/^./) {|match| match.upcase }   "The quick brown fox"        mixed_case("strength in numbers")   "Strength In Numbers"
a.gsub(/[aeiou]/) {|vowel| vowel.upcase }  "thE qUIck brOwn fOx"




                                                                                                                                               3
Classes behind regexps                                      optional args for methods
re = /(d+):(d+)/ # match a time hh:mm                    def cool_dude(arg1="Miles", arg2="Coltrane", arg3="Roach")
re.class -> Regexp                                          "#{arg1}, #{arg2}, #{arg3}."
md = re.match("Time: 12:34am")                             end
md.class   MatchData
md[0]    "12:34"              # $&                         cool_dude   "Miles, Coltrane, Roach."
md[1]    "12"                 # $1                         cool_dude("Bart")    "Bart, Coltrane, Roach."
md[2]    "34"                 # $2                         cool_dude("Bart", "Elwood")     "Bart, Elwood, Roach."
md.pre_match     "Time: ”     # $`                         cool_dude("Bart", "Elwood", "Linus")     "Bart, Elwood, Linus."
md.post_match     "am"        # $’




       Variable number of args                                            code blocks again
def varargs(arg1, *rest)                                   def take_block(p1)
 "Got #{arg1} and #{rest.join(', ')}"                       if block_given?
end                                                           yield(p1)
                                                            else
varargs("one")    "Got one and "                             p1
varargs("one", "two")     "Got one and two"                 end
varargs "one", "two", "three"   "Got one and two, three"   end

                                                           take_block("no block")     "no block"
                                                           take_block("no block") {|s| s.sub(/no /, ‘’) }   "block"




         Capture block explicitly                                          Calling a method
class TaxCalculator                                        connection.download_MP3("jitterbug") {|p| show_progress(p) }
 def initialize(name, &block)
   @name, @block = name, block                             File.size("testfile") 66
 end                                                       Math.sin(Math::PI/4)    0.707106781186548
 def get_tax(amount)
   "#@name on #{amount} = #{ @block.call(amount) }"
                                                           self.class    Object
 end
                                                           self.frozen?    false
end
                                                           frozen?     false
tc = TaxCalculator.new("Sales tax") {|amt| amt * 0.075 }   self.object_id    969948
tc.get_tax(100)    "Sales tax on 100 = 7.5"                object_id     969948
tc.get_tax(250)    "Sales tax on 250 = 18.75"




                                                                                                                             4
Multiple return values                            Expanding arrays into args
def some_method                                           def five(a, b, c, d, e)
 100.times do |num|                                        "I was passed #{a} #{b} #{c} #{d} #{e}"
  square = num*num                                        end
  return num, square if square > 1000
 end
                                                          five(1, 2, 3, 4, 5 )       "I was passed 1 2 3 4 5"
end
                                                          five(1, 2, 3, *['a', 'b'])      "I was passed 1 2 3 a b"
                                                          five(*(10..14).to_a)          "I was passed 10 11 12 13 14"
some_method           [32, 1024]

num, square = some_method
num    32
square    1024




         & for procedure objects                                 Keyword args: use Hash
print "(t)imes or (p)lus: "                               class SongList
times = gets                                               def create_search(name, params)
print "number: "
                                                             # ...
number = Integer(gets)
if times =~ /^t/                                           end
  calc = lambda {|n| n*number }                           end
else
  calc = lambda {|n| n+number }
                                                          list.create_search("short jazz songs", { 'genre' => "jazz",
end
                                                              'duration_less_than' => 270 })
puts((1..10).collect(&calc).join(", "))

(t)imes or (p)lus: t                                      list.create_search('short jazz songs', 'genre' => 'jazz',
number: 2                                                     'duration_less_than' => 270)
2, 4, 6, 8, 10, 12, 14, 16, 18, 20




                     Expression fun                                      More expressions
a=b=c=0              0                                    rating = case votes_cast
[ 3, 1, 7, 0 ].sort.reverse     [7, 3, 1, 0]                       when 0...10 then Rating::SkipThisOne
song_type = if song.mp3_type == MP3::Jazz                          when 10...50 then Rating::CouldDoBetter
                 if song.written < Date.new(1935, 1, 1)            else Rating::Rave
                    Song::TradJazz                                 end
                 else
                    Song::Jazz
                 end                                      command expressions:
                else                                      `date` "Mon Jan 16 22:32:17 CST 2006n"
                 Song::Other
                end




                                                                                                                        5

Weitere ähnliche Inhalte

Was ist angesagt?

Perl programming language
Perl programming languagePerl programming language
Perl programming language
Elie Obeid
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
Dave Cross
 

Was ist angesagt? (19)

Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 
Introduction to Perl Best Practices
Introduction to Perl Best PracticesIntroduction to Perl Best Practices
Introduction to Perl Best Practices
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev Assistant
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
ruby1_6up
ruby1_6upruby1_6up
ruby1_6up
 
Sed tips and_tricks
Sed tips and_tricksSed tips and_tricks
Sed tips and_tricks
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Perl
PerlPerl
Perl
 
Hadoop Pig
Hadoop PigHadoop Pig
Hadoop Pig
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 

Andere mochten auch

Стенограмма доклада: «Коммуникации с заказчиком и проектной командой при сбор...
Стенограмма доклада: «Коммуникации с заказчиком и проектной командой при сбор...Стенограмма доклада: «Коммуникации с заказчиком и проектной командой при сбор...
Стенограмма доклада: «Коммуникации с заказчиком и проектной командой при сбор...
Alexander Novichkov
 
Hypocrite Dr. Shriniwas Kashalikar
Hypocrite Dr. Shriniwas KashalikarHypocrite Dr. Shriniwas Kashalikar
Hypocrite Dr. Shriniwas Kashalikar
drsolapurkar
 
Jdbc Odbc使用Excel作数据源
Jdbc Odbc使用Excel作数据源Jdbc Odbc使用Excel作数据源
Jdbc Odbc使用Excel作数据源
yiditushe
 
Java面试知识
Java面试知识Java面试知识
Java面试知识
yiditushe
 
Jni攻略之九――操作异常
Jni攻略之九――操作异常Jni攻略之九――操作异常
Jni攻略之九――操作异常
yiditushe
 
Php和My Sql Web开发(原书第三版)
Php和My Sql Web开发(原书第三版)Php和My Sql Web开发(原书第三版)
Php和My Sql Web开发(原书第三版)
yiditushe
 
Java基础方面
Java基础方面Java基础方面
Java基础方面
yiditushe
 
M A U N A ( S I L E N C E) & S U P E R L I V I N G D R S H R I N I W A S ...
M A U N A ( S I L E N C E) &  S U P E R L I V I N G   D R  S H R I N I W A S ...M A U N A ( S I L E N C E) &  S U P E R L I V I N G   D R  S H R I N I W A S ...
M A U N A ( S I L E N C E) & S U P E R L I V I N G D R S H R I N I W A S ...
drsolapurkar
 

Andere mochten auch (16)

Winter09TECH
Winter09TECHWinter09TECH
Winter09TECH
 
Стенограмма доклада: «Коммуникации с заказчиком и проектной командой при сбор...
Стенограмма доклада: «Коммуникации с заказчиком и проектной командой при сбор...Стенограмма доклада: «Коммуникации с заказчиком и проектной командой при сбор...
Стенограмма доклада: «Коммуникации с заказчиком и проектной командой при сбор...
 
Hypocrite Dr. Shriniwas Kashalikar
Hypocrite Dr. Shriniwas KashalikarHypocrite Dr. Shriniwas Kashalikar
Hypocrite Dr. Shriniwas Kashalikar
 
Jdbc Odbc使用Excel作数据源
Jdbc Odbc使用Excel作数据源Jdbc Odbc使用Excel作数据源
Jdbc Odbc使用Excel作数据源
 
Australia
AustraliaAustralia
Australia
 
Resume92816
Resume92816Resume92816
Resume92816
 
M. GIEBEL RESUME.PDF
M. GIEBEL RESUME.PDFM. GIEBEL RESUME.PDF
M. GIEBEL RESUME.PDF
 
Java面试知识
Java面试知识Java面试知识
Java面试知识
 
Prototype-1
Prototype-1Prototype-1
Prototype-1
 
Jni攻略之九――操作异常
Jni攻略之九――操作异常Jni攻略之九――操作异常
Jni攻略之九――操作异常
 
Wapflow
WapflowWapflow
Wapflow
 
11 18 Fencing
11 18 Fencing11 18 Fencing
11 18 Fencing
 
Php和My Sql Web开发(原书第三版)
Php和My Sql Web开发(原书第三版)Php和My Sql Web开发(原书第三版)
Php和My Sql Web开发(原书第三版)
 
Java基础方面
Java基础方面Java基础方面
Java基础方面
 
M A U N A ( S I L E N C E) & S U P E R L I V I N G D R S H R I N I W A S ...
M A U N A ( S I L E N C E) &  S U P E R L I V I N G   D R  S H R I N I W A S ...M A U N A ( S I L E N C E) &  S U P E R L I V I N G   D R  S H R I N I W A S ...
M A U N A ( S I L E N C E) & S U P E R L I V I N G D R S H R I N I W A S ...
 
FW: ARTE C ON LÀPICES
FW: ARTE C ON LÀPICES FW: ARTE C ON LÀPICES
FW: ARTE C ON LÀPICES
 

Ähnlich wie ruby3_6up

Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
Wen-Tien Chang
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
Kang-min Liu
 

Ähnlich wie ruby3_6up (20)

Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Groovy intro for OUDL
Groovy intro for OUDLGroovy intro for OUDL
Groovy intro for OUDL
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Ruby_Basic
Ruby_BasicRuby_Basic
Ruby_Basic
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
 
Array,lists and hashes in perl
Array,lists and hashes in perlArray,lists and hashes in perl
Array,lists and hashes in perl
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Dades i operadors
Dades i operadorsDades i operadors
Dades i operadors
 
Attributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active recordAttributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active record
 
Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a boss
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 

Mehr von tutorialsruby

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
tutorialsruby
 

Mehr von tutorialsruby (20)

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
CSS
CSSCSS
CSS
 
CSS
CSSCSS
CSS
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 

Kürzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

ruby3_6up

  • 1. Index by each word A quick Ruby Tutorial, Part 3 class WordIndex def initialize @index = {} end def add_to_index(obj, *phrases) phrases.each do |phrase| phrase.scan(/w[-w']+/) do |word| # extract each word word.downcase! COMP313 @index[word] = [] if @index[word].nil? @index[word].push(obj) Source: Programming Ruby, The Pragmatic end end Programmers’ Guide by Dave Thomas, Chad end def lookup(word) Fowler, and Andy Hunt @index[word.downcase] end end Add full index to SongList Ranges class SongList 1..10 def initialize 'a'..'z' @songs = Array.new my_array = [ 1, 2, 3 ] @index = WordIndex.new 0...my_array.length end def append(song) (1..10).to_a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] @songs.push(song) ('bar'..'bat').to_a ["bar", "bas", "bat"] @index.add_to_index(song, song.name, song.artist) self digits = 0..9 end digits.include?(5) true def lookup(word) digits.min 0 @index.lookup(word) digits.max 9 end digits.reject {|i| i < 5 } [5, 6, 7, 8, 9] end digits.each {|digit| dial(digit) } 0..9 Range example: VU meter class VU VU Ranges / Ranges in conditions include Comparable attr :volume def initialize(volume) # 0..9 medium_volume = VU.new(4)..VU.new(7) @volume = volume medium_volume.to_a [####, #####, ######, #######] end medium_volume.include?(VU.new(3)) false def inspect '#' * @volume end (1..10) === 5 true # Support for ranges (1..10) === 15 false def <=>(other) self.volume <=> other.volume (1..10) === 3.14159 true end ('a'..'j') === 'c' true def succ raise(IndexError, "Volume too big") if @volume >= 9 ('a'..'j') === 'z' false VU.new(@volume.succ) end end 1
  • 2. Regular expressions Special vars $` $& $’ a = Regexp.new('^s*[a-z]') /^s*[a-z]/ def show_regexp(a, re) if a =~ re b = /^s*[a-z]/ /^s*[a-z]/ "#{$`}<<#{$&}>>#{$'}" c = %r{^s*[a-z]} /^s*[a-z]/ else "no match" end end name = "Fats Waller" name =~ /a/ 1 show_regexp('very interesting', /t/) very in<<t>>eresting show_regexp('Fats Waller', /a/) F<<a>>ts Waller name =~ /z/ nil show_regexp('Fats Waller', /ll/) Fats Wa<<ll>>er /a/ =~ name 1 show_regexp('Fats Waller', /z/) no match Special chars, anchors More RegExps ., |, (, ), [, ], {, }, +, , ^, $, *, and ? show_regexp('Price $12.', /[aeiou]/) Pr<<i>>ce $12. show_regexp('Price $12.', /[s]/) Price<< >>$12. show_regexp('kangaroo', /angar/) k<<angar>>oo show_regexp('Price $12.', /[[:digit:]]/) Price $<<1>>2. show_regexp('Price $12.', /[[:space:]]/) Price<< >>$12. show_regexp('!@%&-_=+', /%&/) !@<<%&>>-_=+ show_regexp('Price $12.', /[[:punct:]aeiou]/) Pr<<i>>ce $12. show_regexp("this isnthe time", /^the/) this isn<<the>> time a = 'see [Design Patterns-page 123]' show_regexp("this isnthe time", /is$/) this <<is>>nthe time show_regexp(a, /[A-F]/) see [<<D>>esign Patterns-page 123] show_regexp("this isnthe time", /Athis/) <<this>> isnthe time show_regexp(a, /[A-Fa-f]/) s<<e>>e [Design Patterns-page 123] show_regexp("this isnthe time", /Athe/) no match show_regexp(a, /[0-9]/) see [Design Patterns-page <<1>>23] show_regexp(a, /[0-9][0-9]/) see [Design Patterns-page <<12>>3] character classes Repetition d [0-9] Digit character r* matches zero or more occurrences of r. D [^0-9] Any character except a digit r+ matches one or more occurrences of r. s [ trnf] Whitespace character r? matches zero or one occurrence of r. S [^ trnf] Any character except whitespace r{m,n} matches at least m and at most n occurrences of r. w [A-Za-z0-9_] Word character r{m,} matches at least m occurrences of r. W [^A-Za-z0-9_] Any character except a word character r{m} matches exactly m occurrences of r. /ab+/ matches ab, abb, abbbb, … /(ab)+/ matches ab, abab, ababab, … /a*/ matches everything (why?) 2
  • 3. Greedy matching/Alternatives Groupings a = "The moon is made of cheese" show_regexp('banana', /an*/) b<<an>>ana show_regexp(a, /w+/) <<The>> moon is made of cheese show_regexp('banana', /(an)*/) <<>>banana show_regexp(a, /s.*s/) The<< moon is made of >>cheese show_regexp('banana', /(an)+/) b<<anan>>a show_regexp(a, /s.*?s/) The<< moon >>is made of cheese a = 'red ball blue sky' show_regexp(a, /[aeiou]{2,99}/) The m<<oo>>n is made of show_regexp(a, /blue|red/) <<red>> ball blue sky cheese show_regexp(a, /(blue|red) w+/) <<red ball>> blue sky show_regexp(a, /(red|blue) w+/) <<red ball>> blue sky a = "red ball blue sky" show_regexp(a, /red|blue w+/) <<red>> ball blue sky show_regexp(a, /d|e/) r<<e>>d ball blue sky show_regexp(a, /red (ball|angry) sky/) no match a = 'the red angry sky' show_regexp(a, /al|lu/) red b<<al>>l blue sky show_regexp(a, /red (ball|angry) sky/) the <<red angry sky>> show_regexp(a, /red ball|angry sky/) <<red ball>> blue sky Groupings collect matches Match inside with 1,… "12:50am" =~ /(dd):(dd)(..)/ 0 # match duplicated letter "Hour is #$1, minute #$2" "Hour is 12, minute 50" show_regexp('He said "Hello"', /(w)1/) He said "He<<ll>>o" # match duplicated substrings "12:50am" =~ /((dd):(dd))(..)/ 0 show_regexp('Mississippi', /(w+)1/) M<<ississ>>ippi "Time is #$1" "Time is 12:50" "Hour is #$2, minute #$3" "Hour is 12, minute 50" show_regexp('He said "Hello"', /(["']).*?1/) He said <<"Hello">> "AM/PM is #$4" "AM/PM is am" show_regexp("He said 'Hello'", /(["']).*?1/) He said <<'Hello'>> Substitute patterns Upcase every first char a = "the quick brown fox" def mixed_case(name) a.sub(/[aeiou]/, '*') "th* quick brown fox" name.gsub(/bw/) {|word| word.upcase } a.gsub(/[aeiou]/, '*') "th* q**ck br*wn f*x" end a.sub(/sS+/, '') "the brown fox" a.gsub(/sS+/, '') "the" mixed_case("fats waller") "Fats Waller" mixed_case("louis armstrong") "Louis Armstrong" a.sub(/^./) {|match| match.upcase } "The quick brown fox" mixed_case("strength in numbers") "Strength In Numbers" a.gsub(/[aeiou]/) {|vowel| vowel.upcase } "thE qUIck brOwn fOx" 3
  • 4. Classes behind regexps optional args for methods re = /(d+):(d+)/ # match a time hh:mm def cool_dude(arg1="Miles", arg2="Coltrane", arg3="Roach") re.class -> Regexp "#{arg1}, #{arg2}, #{arg3}." md = re.match("Time: 12:34am") end md.class MatchData md[0] "12:34" # $& cool_dude "Miles, Coltrane, Roach." md[1] "12" # $1 cool_dude("Bart") "Bart, Coltrane, Roach." md[2] "34" # $2 cool_dude("Bart", "Elwood") "Bart, Elwood, Roach." md.pre_match "Time: ” # $` cool_dude("Bart", "Elwood", "Linus") "Bart, Elwood, Linus." md.post_match "am" # $’ Variable number of args code blocks again def varargs(arg1, *rest) def take_block(p1) "Got #{arg1} and #{rest.join(', ')}" if block_given? end yield(p1) else varargs("one") "Got one and " p1 varargs("one", "two") "Got one and two" end varargs "one", "two", "three" "Got one and two, three" end take_block("no block") "no block" take_block("no block") {|s| s.sub(/no /, ‘’) } "block" Capture block explicitly Calling a method class TaxCalculator connection.download_MP3("jitterbug") {|p| show_progress(p) } def initialize(name, &block) @name, @block = name, block File.size("testfile") 66 end Math.sin(Math::PI/4) 0.707106781186548 def get_tax(amount) "#@name on #{amount} = #{ @block.call(amount) }" self.class Object end self.frozen? false end frozen? false tc = TaxCalculator.new("Sales tax") {|amt| amt * 0.075 } self.object_id 969948 tc.get_tax(100) "Sales tax on 100 = 7.5" object_id 969948 tc.get_tax(250) "Sales tax on 250 = 18.75" 4
  • 5. Multiple return values Expanding arrays into args def some_method def five(a, b, c, d, e) 100.times do |num| "I was passed #{a} #{b} #{c} #{d} #{e}" square = num*num end return num, square if square > 1000 end five(1, 2, 3, 4, 5 ) "I was passed 1 2 3 4 5" end five(1, 2, 3, *['a', 'b']) "I was passed 1 2 3 a b" five(*(10..14).to_a) "I was passed 10 11 12 13 14" some_method [32, 1024] num, square = some_method num 32 square 1024 & for procedure objects Keyword args: use Hash print "(t)imes or (p)lus: " class SongList times = gets def create_search(name, params) print "number: " # ... number = Integer(gets) if times =~ /^t/ end calc = lambda {|n| n*number } end else calc = lambda {|n| n+number } list.create_search("short jazz songs", { 'genre' => "jazz", end 'duration_less_than' => 270 }) puts((1..10).collect(&calc).join(", ")) (t)imes or (p)lus: t list.create_search('short jazz songs', 'genre' => 'jazz', number: 2 'duration_less_than' => 270) 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 Expression fun More expressions a=b=c=0 0 rating = case votes_cast [ 3, 1, 7, 0 ].sort.reverse [7, 3, 1, 0] when 0...10 then Rating::SkipThisOne song_type = if song.mp3_type == MP3::Jazz when 10...50 then Rating::CouldDoBetter if song.written < Date.new(1935, 1, 1) else Rating::Rave Song::TradJazz end else Song::Jazz end command expressions: else `date` "Mon Jan 16 22:32:17 CST 2006n" Song::Other end 5