LoginSignup
0
0

More than 3 years have passed since last update.

変数とif文

Last updated at Posted at 2021-01-25

userが存在するかどうか判別が可能

> user = User.first
=> #<User id: 1, name: "Michael Hartl", email: "michael@example.com", created_at:, updated_at:, password_digest: >

> if user
>   puts "user exist"
> else
>   puts "user don't exist"
> end
user exist
=> nil

irb(main):012:0> user = nil
=> nil

> if user
>   puts "user exist"
> else
>   puts "user don't exist"
> end
user dont exist
=> nil

またuserのパスワードがfoobarの場合
authenticateメソッドを使うことでパスワードと一致してるか検証可能

> user = User.first
=> #<User id: 1, name: "Michael Hartl", email: "michael@example.com", created_at:, updated_at:, password_digest: FILTERED]>

> if user.authenticate("foobar")
>   puts "Password is correct"
> else
>   puts "Password is wrong"
> end
Password is correct
=> nil

> if user.authenticate("")
>   puts "Password is correct"
> else
>   puts "Password is wrong"
> end
Password is wrong
=> nil
0
0
0

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
0
0