WEB+DB PRESS Vol.96 読了

WEB+DB PRESS Vol.96

WEB+DB PRESS Vol.96

今回はなんと、はやぶさ2 の特集がありました。
技術的な話は全然理解できてませんが、とんでもなく大変なことをやっているというのは理解できました。
地球の重力を使って衛星を加速させる「地球スイングバイ」とか、太陽フレアを考慮した誤り制御とか、地上時刻変換とか とにかくすごい話盛りだくさんでした。

時々、ドラクエとかWEB+DBっぽくない特集がされるのも良いですね。
WEB+DB PRESS Vol.90 ドラゴンクエストX開発ノウハウ大公開 - rochefort’s blog
 
その他、沢山気になる記事がありました。

私のキャリアチェンジ

個人的にペパポCTOの栗林さんのお話が良かったです。

何度も見る夢

最近でもそうなのですが、能力不足によりエンジニアを辞めざるを得ず、
地元に帰らざるを得ない状況になるも、田舎にはやれる仕事もなく、
この先どうやって生きていけばいいのだろうと困惑する夢を何度も見ます。

不安とどう付き合うか

Web業界は、定年退職した人がまだいない業界です。
あれこれと悩むのは無意味です。そんな暇があるなら、
一行でも多くコードを書く方がよいでしょう。

Ruby2.4最速入門

Integer統合、Binding#irb などなど沢山変更が入っています。
 
以下、その他個人的に便利そうだなと思ったやつ。

Logger.newのキーワード引数

Logger.newにキーワード引数のlevel,progname,formatter,datetime_formatが追加。

require 'logger'

formatter = proc { |severity, timestamp, progname, msg|
  "#{severity}: [#{timestamp}] #{progname}: #{msg}\n"
}

logger = Logger.new(STDOUT,
  level: :info,
  progname: 'DEMO1',
  datetime_format: "%d-%b-%Y@%H:%M:%S",
  formatter: formatter
)

# 実行結果
logger.info("test")
INFO: [2017-04-08 22:39:16 +0900] DEMO1: test

chompフラグ

IO#gets/readline/each_line/readlines/foreach にchompフラグが追加。

File.open("sample.txt") { |f| f.readlines(chomp: true) }

ECMAScript 2015/2016 コーディングのベストプラクティス

既存コードの対比付きで、わかりやすくて良かったです。
Promise、importを使ったmodule化など。

アロー関数

  • アロー関数はthisの値が関数の外側と変わらない。普通の関数は状況によって変わる。
  • アロー関数はnewできない。普通の関数はできる。
  • 普通の関数のみ巻き上げがある。

WEB+DB PRESS Vol.95 読了

WEB+DB PRESS Vol.95

WEB+DB PRESS Vol.95

アクセシビリティ、GO、HTTP、Serverless Framework、Angular2など。
最近読んだ Vol.90-94 に比べて、個人的に気になる話が少なかった。
 
HTTPの特集は、最後はHTTP2の話があったのが良かった。
Serverless Frameworkは雰囲気が掴めて良かった。チュートリアルやって見たのですが、デバッグ大変だなぁ。
 

余談

ncコマンド

HTTPの話で出てきた nc コマンドの利用例。こんなコマンドあったんですね。

$ nc -c example.com 80
GET /index.html HTTP/1.1
Host: localhsot
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Type: text/html
Date: Sun, 09 Apr 2017 09:29:37 GMT
Last-Modified: Sun, 09 Apr 2017 08:36:12 GMT
Server: ECS (pae/37AE)
Content-Length: 94

<html><head><title>edgecastcdn.net</title></head><body><h1>edgecastcdn.net</h1></body></html>

Strings and arrows(CODEEVAL)

文字列に含まれる全てのパターンをカウントする問題。
count_all_pattern がごちゃっとしてしまっている。もう少し良い方法がありそう。

CHALLENGE DESCRIPTION:

You have a string composed of the following symbols: ‘>’, ‘<’, and ‘-’. Your task is to find, count, and print to the output a number of arrows in the string. An arrow is a set of the following symbols: ‘>>–>’ or ‘<–<<’.
Note that one character may belong to two arrows at the same time. Such example is shown in the line #1.

INPUT SAMPLE:

The first argument is a path to a file. Each line includes a test case with a string of different length from 10 to 250 characters. The string consists of ‘>’, ‘<’, and ‘-’ symbols.

<--<<--<<
<<>>--><--<<--<<>>>--><
<-->>

OUTPUT SAMPLE:

2
4
0

CONSTRAINTS:

  1. An arrow is a set of the following symbols: ‘>>–>’ or ‘<–<<’.
  2. One symbol may belong to two arrows at the same time.
  3. The number of test cases is 40.

My Code

#!/usr/bin/env ruby -w

ARROWS = [">>-->", "<--<<"]

def count_arrows(str)
  ARROWS.inject(0) do |count, arrow|
    count += count_all_pattern(str, arrow)
  end
end

def count_all_pattern(str, pattern)
  i = 0
  count = 0
  str_size = str.size
  while i < str_size
    index = str.index(pattern, i)
    unless index.nil?
      i = index
      count += 1
    end
    i += 1
  end
  count
end

ARGF.each_line do |line|
  puts count_arrows(line.chomp)
end