Class: Common

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_learner/common.rb

Instance Method Summary collapse

Instance Method Details

#change_theme(color: String, gem_dir: String) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby_learner/common.rb', line 49

def change_theme(color: String, gem_dir: String)
  chmoded = 0
  file_path = "#{gem_dir}/lib/datas/theme_color.txt"
  begin
    File.write(file_path, "#{color}")
    puts "your ruby_learner's color is #{color}!!"
  rescue => error
    system "sudo chmod go+w #{file_path}"
    chmoded += 1
    retry if chmoded < 2
    puts "FileWrite error #{error.message}"
    puts "you should input $sudo chmod go+w #{file_path}"
  end
end

#get_app_ver(app_name: String) ⇒ Object



43
44
45
46
47
# File 'lib/ruby_learner/common.rb', line 43

def get_app_ver(app_name: String)
  app_vers = Open3.capture3("gem list #{app_name}")
  latest_ver = app_vers[0].chomp.gsub(' (', '-').gsub(')','')
  return latest_ver
end

#init_mk_files(gem_dir: String, workshop_dir: String) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/ruby_learner/common.rb', line 33

def init_mk_files(gem_dir: String, workshop_dir: String)
  if Dir.exist?(workshop_dir) != true then
    FileUtils.mkdir_p(workshop_dir)
    system("cp -R #{gem_dir}/workshop/* #{workshop_dir}")
    system("cd #{workshop_dir} && mv emacs.d .emacs.d")
    system("cd #{workshop_dir} && mv rspec .rspec")
    system("cd #{workshop_dir} && mv rubocop.yml .rubocop.yml")
  end
end

#instruct_printObject



27
28
29
30
31
# File 'lib/ruby_learner/common.rb', line 27

def instruct_print
  puts "continue >>> [RET]"
  puts "stop >>> 'exit' + [RET]"
  puts "check answer >>> 'answer' + [RET]"
end

#restore(file: String, workshop_dir: String) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby_learner/common.rb', line 2

def restore(file: String, workshop_dir: String)
restore_file = ""
  line = File.open("#{workshop_dir}/lib/answer.rb") do |f|
    1.times {
      f.gets
    }
    restore_file = f.gets
  end
  restore_file.gsub!(" ", "")
  restore_file.delete!("#")
  Dir::chdir("#{workshop_dir}/restore"){
    file_count = Dir.glob("*.rb").count
    # restore_file.insert(-5, "[#{file_count}]")
    restore_file.insert(0, "[#{file_count}]")
  }
  system "touch #{workshop_dir}/restore/#{restore_file}"
  system "cp #{workshop_dir}/lib/workplace.rb #{workshop_dir}/restore/#{restore_file}"
end

#time_check(start_time: Time) ⇒ Object



21
22
23
24
25
# File 'lib/ruby_learner/common.rb', line 21

def time_check(start_time: Time)
  end_time = Time.now
  elapsed_time = end_time - start_time - 1
  return elapsed_time
end