Techbay / study

1 stars 0 forks source link

NO.1 #1

Open iannono opened 8 years ago

iannono commented 8 years ago
hlee commented 8 years ago

finished 409 optional Gem learned active gem, and load is different and major version

gem "clipboard", "~> 1.0"

iannono commented 8 years ago

406 last match

format from this

shodan: INFO Mining laser maintenance cycle finished
shodan: INFO Reactor nominal
shodan: WARN Unverified access
shodan: INFO Grove climate control day cycle
shodan: WARN Ethics node offline
shodan: ERROR Notification of station IT failed
shodan: INFO Science level security protocols updated
shodan: ERROR Uncontrolled neural net reconfiguration
shodan: ERROR Safety protocols inconsistent with updated mandates
shodan: FATAL Logging subsystem compromi

to this:

* Notification of station IT failed
* Uncontrolled neural net reconfiguration
* Safety protocols inconsistent with updated mandates
! Logging subsystem compromi

step 1

sigils = { "ERROR" => "*", "FATAL" => "!", "WARN" => "?" }
output = open("shodan.log").each_line.grep(/ERROR|FATAL/).map{|line|
  level   = line[/ERROR|FATAL/]
  message = line[/(ERROR|FATAL) (.*)/, 2]
  "#{sigils[level]} #{message}"
}

step 2

sigils = { "ERROR" => "*", "FATAL" => "!", "WARN" => "?" }
output = open("shodan.log").each_line.grep(/ERROR|FATAL/) {|line|
  level   = line[/ERROR|FATAL/]
  message = line[/(ERROR|FATAL) (.*)/, 2]
  "#{sigils[level]} #{message}"
}

step 3

sigils = { "ERROR" => "*", "FATAL" => "!", "WARN" => "?" }
output = open("shodan.log").each_line.grep(/ERROR|FATAL/) {|line|
  match   = line.match(/(ERROR|FATAL) (.*)/)
  level   = match[1]
  message = match[2]
  "#{sigils[level]} #{message}"
}

step 4

sigils = { "ERROR" => "*", "FATAL" => "!", "WARN" => "?" }
output = open("shodan.log").each_line.grep(/(ERROR|FATAL) (.*)/) {|line|
  #<MatchData "ERROR Notification of station IT failed" 1:"ERROR" 2:"Notification of station IT failed">
  match   = Regexp.last_match
  # "ERROR"
  level   = match[1]
  "Notification of station IT failed"
  message = match[2]
  "#{sigils[level]} #{message}"
}

step 5

sigils = { "ERROR" => "*", "FATAL" => "!", "WARN" => "?" }
output = open("shodan.log").each_line.grep(/ERROR|FATAL/) {|line|
  # "ERROR"
  level   = Regexp.last_match.to_s
  # "Notification of station IT failed"
  message = Regexp.last_match.post_match.strip
  "#{sigils[level]} #{message}"
}

step 6

match = Regexp.last_match         # => #<MatchData "ERROR">
$~                                # => #<MatchData "ERROR">
match.pre_match                   # => "shodan: "
$`                                # => "shodan: "
match.to_s                        # => "ERROR"
$&                                # => "ERROR"
match.post_match                  # => " Notification of station IT failed"
$'                                # => " Notification of station IT failed"

sigils = { "ERROR" => "*", "FATAL" => "!", "WARN" => "?" }
output = open("shodan.log").each_line.grep(/ERROR|FATAL/) {|line|
  "#{sigils[$&]} #{$'.strip}"
}

Primitive Obsession

class Course
    attr_accessor :title, :duration

    def initialize(title: title, duration: duration)
      @title      = title
      @duration   = duration
    end

    def duration_in_months
      @duration / 28
    end

    def duration_in_weeks
      @duration / 7
    end

    def duration_in_days
      @duration
    end
  end

COURSES = {
  potions: Course.new(title: "Potions 101", duration: 90),
  scrying: Course.new(title: "Scrying 101", duration: 21),
  hexes: Course.new(title: "Hexes", duration: 3),
}
render_course_info(COURSES[:hexes])
# => "Hexes (3 days)"
render_course_info(COURSES[:potions])
# => "Potions 101 (3 months)"

def render_course_info(course)
  "#{course.title} (#{render_course_duration(course)})"
end

def render_course_duration(course)
  if course.duration < 14
    "#{course.duration_in_days} days"
  elsif course.duration_in_days < 60
    "#{course.duration_in_weeks} weeks"
  else
    "#{course.duration_in_months} months"
  end
end

Whole Value

class Duration
  def self.[](count)
    self.new(count)
  end

  attr_reader :magnitude

  def initialize(magnitude)
    @magnitude = magnitude
    freeze
  end

  def inspect
    "#{self.class}[#{magnitude}]"
  end

  def to_s
    "#{magnitude} #{self.class.name.downcase}"
  end

  alias_method :to_i, :magnitude
end

class Days < Duration; end
class Weeks < Duration; end
class Months < Duration; end

# Months[2], Weeks[3]

class Course
  attr_accessor :title, :duration

  def initialize(title: title, duration: duration)
    @title      = title
    @duration   = duration
  end
end

COURSES = {
  potions: Course.new(title: "Potions 101", duration: Months[3]),
  scrying: Course.new(title: "Scrying 101", duration: Weeks[3]),
  hexes: Course.new(title: "Hexes", duration: Days[3]),
}

def render_course_info(course)
  "#{course.title} (#{course.duration})"
end

render_course_info(COURSES[:hexes])
# => "Hexes (3 days)"
render_course_info(COURSES[:potions])
# => "Potions 101 (3 months)"