Skip to content

Instantly share code, notes, and snippets.

@atomical
Created November 18, 2015 19:59
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 atomical/5b293204094df4128b46 to your computer and use it in GitHub Desktop.
Save atomical/5b293204094df4128b46 to your computer and use it in GitHub Desktop.
[13:56:52] <leitz> In an attempt to graduate Coder Kindergarden, any suggestions on improving the code? #!/usr/bin/env ruby
[13:56:52] <leitz> # edit_json.rb
[13:56:52] <leitz> # 0.0.1
[13:56:52] <leitz> require 'json'
[13:56:52] <leitz> $my_hash = Hash.new
[13:56:54] <leitz> def valid_json?(json)
[13:56:56] <leitz> begin
[13:56:58] <leitz> data = JSON.parse(json)
[13:57:00] <leitz> return data, true
[13:57:04] <leitz> rescue Exception => e
[13:57:06] <leitz> return false
[13:57:08] <leitz> end
[13:57:10] <leitz> end
[13:57:12] <leitz> def show_keys(hash)
[13:57:13] <newdan> leitz: Use dpaste.org or something
[13:57:14] <leitz> hash_keys = Array.new
[13:57:16] <leitz> hash.each_key do |key|
[13:57:19] <newdan> leitz: Don't paste code right into IRC
[13:57:20] <leitz> hash_keys << key
[13:57:24] <leitz> end
[13:57:26] <leitz> hash_keys.sort!
[13:57:28] <leitz> return hash_keys
[13:57:30] <leitz> end
[13:57:30] mistermocha (~mistermoc@8.25.195.25) left IRC (Ping timeout: 240 seconds)
[13:57:34] <leitz> def edit_key(key)
[13:57:36] <leitz> if $my_hash.has_key?(key)
[13:57:38] <leitz> puts "Okay, we'll edit #{key}."
[13:57:40] <leitz> subkeys = Array.new
[13:57:42] <leitz> $my_hash[key].each_key do |subkey|
[13:57:44] <leitz> subkeys << subkey
[13:57:46] <leitz> end
[13:57:50] <leitz> subkeys.sort!
[13:57:54] <leitz> subkeys.each do |sk|
[13:57:56] <leitz> print "#{sk} "
[13:57:58] demonlove (~greg@27.107.225.57) joined the channel
[13:57:58] <leitz> end
[13:58:01] <leitz> puts
[13:58:04] <leitz> puts "Which subkey do you want to edit?"
[13:58:06] <leitz> subkey_to_edit = STDIN.gets.chomp
[13:58:08] <leitz> puts "Okay, fixing #{subkey_to_edit}."
[13:58:09] <centrx> What are you doing
[13:58:10] <leitz> puts "That subkey is currently #{$my_hash[key][subkey_to_edit]}."
[13:58:11] <atomical> are you live coding?
[13:58:11] <Mon_Ouie> !kick leitz
[13:58:12] ChanServ sets mode +o ruboto
[13:58:12] ruboto kicked leitz from the channel (leitz)
[13:58:12] ruboto sets mode -o ruboto
[13:58:23] <newdan> Haha thank you Mon_Ouie
[13:58:38] leitz (~leitz@c-71-62-66-192.hsd1.va.comcast.net) joined the channel
[13:58:40] <leitz> if valid
[13:58:42] <leitz> keys_list = show_keys($my_hash)
[13:58:44] <leitz> puts "There are #{keys_list.count} keys. Which would you like to edit?"
[13:58:46] al2o3-cr (~al2o3-cr@unaffiliated/gr33n7007h) left IRC (Ping timeout: 240 seconds)
[13:58:46] <centrx> You just failed Coder Kindergarten
[13:58:46] <leitz> STDOUT.flush
[13:58:46] <newdan> Wtf
[13:58:50] idefine (~idefine@204.148.42.2) joined the channel
[13:58:50] <leitz> key_to_edit = STDIN.gets.chomp
[13:58:53] <Mon_Ouie> !mute leitz 5m
[13:58:53] ChanServ sets mode +o ruboto
[13:58:53] ruboto sets mode +q leitz!*@*
[13:58:53] ruboto sets mode -o ruboto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment