quattro_4 scribble

scribble 落書き (調べた事をただ落書きする)

RubyTapas #152 - #155

152 Progress Bar

★★

require "ruby-progressbar"
...

progress = ProgressBar.create(total: repeat)
repeat.times do
  ...
  progress.increment
end

Progress: |=========================================================================================================================================|

one of the gems that I think every Ruby programmer should have installed in their global gemset, because with just a couple of lines of code it can dramatically improve the user experience of command-line scripts

153 Testing Sleep

★★

#create_thumbnail. This method's job is to make a special request to Wistia

Wistia, the website that hosts the RubyTapas free sample videos

problem of simulating HTTP requests (FakeWeb)

there's the matter of the #sleep

extensible (callers can easily change how long it waits)

def create_thumbnail(url, attributes={}, &on_not_ready)
  on_not_ready ||= -> { sleep 1 }
  ...
    when 202 then on_not_ready.call

inform the user when it is waiting to retry

progress = ProgressBar.create(total: nil)
...
progress.log "Waiting for thumbnail to be created"

154 Testing Threads

★★★

As a general rule,

  • we don't like slow tests
  • we really don't like unreliable tests

use a sleep time of 1 millisecond is probably about the shortest period the VM can accurately sleep

def wait_for
  Timeout.timeout 1 do
    sleep 0.001 until yield
  end
end

155 Matching Digits

★★★

table print

require "table_print"
tp matches, "num", "string", "matches.to_s"

Advanced Regular Expression

zero-width negative lookahead and lookbehind

show_matches(titles, /(?<!\d)\d{3}(?!\d)/, captures: true)

? at the start of the group signals to Ruby that we will be using a special regex extension

< tells it that the extension we want to use is the "lookbehind" feature

! says to make this a negative lookbehind

(must be preceded by something other than a digit. Whether that is a letter, some punctuation, or nothing at all doesn't matter)

?!\d tells Ruby that the triplet must not be followed by a digit

xオプションでスペースや改行を無視するのでコメントを書ける

EPISODE_NUMBER_PATTERN = /
      (?<!\d)                   # preceded by non-digits
      \d{3}                     # exactly three digits in a row
      (?!\d)                    # followed by non-digits
/x
show_matches(titles, EPISODE_NUMBER_PATTERN, captures: true)

Rubular: a Ruby regular expression editor and tester

Nell Shamrell GoGaRuCo 2013 - Beneath the Surface: Regular Expressions in Ruby - YouTube