LoginSignup
3
2

More than 5 years have passed since last update.

RubyのString#matchの挙動

Last updated at Posted at 2016-01-22

RubyのString#matchの挙動に関して。

ユーザーの入力した値のチェックをするために、以下のようなプログラムを書いていたらエラーが出た。

app.rb
require 'bundler/setup'
Bundler.require
require 'sinatra/reloader' if development?
require 'net/http'
get "/" do
    if params[:test].match(/\d+(\.\d+)?/)
        #何らかの処理
    end
end

これでparams[:test]に値が渡されないと、nilに対してString#matchを行うのでエラーがおきてしまった。
以後注意したい。

3
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2