Skip to content

Instantly share code, notes, and snippets.

@nilium

nilium/fail.rb Secret

Last active December 19, 2015 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilium/8566489480d673fa654d to your computer and use it in GitHub Desktop.
Save nilium/8566489480d673fa654d to your computer and use it in GitHub Desktop.
# Should succeed, has TYPE=".."
GOOD_BLKID = %{/dev/vg/root: LABEL="/" UUID="6a1e5f4f-37c9-4809-969a-89cf6725cbbd" TYPE="ext4"}
# No TYPE="ext[34]", shouldn't succeed
BAD_BLKID = %{/dev/vg/root: LABEL="/" UUID="6a1e5f4f-37c9-4809-969a-89cf6725cbbd" TYPE="reiserfs"}
TYPE_REGEX = /TYPE=\"(\S+)\"/
MATCH_EXT34 = /ext[34]/
# With the good blkid, this should return non-nil if it's not faulty
r = TYPE_REGEX.match(GOOD_BLKID) { |m| m =~ MATCH_EXT34 } # => nil
puts r.inspect
# With the bad blkid, this should return nil if it's not faulty
r = TYPE_REGEX.match(BAD_BLKID) { |m| m =~ MATCH_EXT34 } # => nil
puts r.inspect
# In reality, the above will both fail in a normal Ruby environment where =~
# returns nil by default, as MatchData (m.class in the above block) does not
# implement a method for it.
# Both of these should work as expected, rather.
r = TYPE_REGEX.match(GOOD_BLKID) { |m| m[1] =~ MATCH_EXT34 } # => 0
puts r.inspect
r = TYPE_REGEX.match(BAD_BLKID) { |m| m[1] =~ MATCH_EXT34 } # => nil
puts r.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment