Ad
class Person
  attr_accessor :name
  def initialize(name)
    @name = name
  end
end


def change_name(new_name, thing)
  if thing.respond_to?(:name=)
    #  do something
    thing.name = new_name
  else
    nil
  end
end
def array_of_two(n)
  raise 'n is not a number' unless n.is_a?(Numeric)  # comment to fail test case
  [n, n]
end
// const request = require('request');

// function getUserAllies(username){
//   return new Promise(function(resolve, reject){
//     request('https://www.codewars.com/users/'+username, function(err, res, body){
//       let html = body;
//       let allies = (html.match(/<div class=\"stat\"><b>Allies:<\/b>(\d+)<\/div>/) || [,'null match'])[1];
//       resolve(+allies);
//     });
//   });
// }

// using request-promise to avoid wrapping a Promise
const rp = require('request-promise');

function getUserAllies(username){
  return rp('https://www.codewars.com/users/'+username).then(function(body){
    let html = body;
    let allies = (html.match(/<div class=\"stat\"><b>Allies:<\/b>(\d+)<\/div>/) || [,'null match'])[1];
    return(+allies);
  });
}

Use snippets in Test Cases and Preloaded sections.

# Uncomment method redefinitions and errors will be thrown(can't publish with errors). (ノ^_^)ノ┻━┻
class NilClass
  # def ==(o)
  #   true
  # end
  # def !=(o)
  #   true
  # end
end

class Object
  # def ==(o)
  #   true
  # end
  # def !=(o)
  #   true
  # end
end

def method
  # nil
end
Code
Diff
  • const package = require('fs').readFileSync('../../runner/package.json','utf8');
    
    console.log(package);
    
    • const exec = require('child_process').exec;
    • const package = require('fs').readFileSync('../../runner/package.json','utf8');
    • function parse(error, stdout, stderr) {
    • if (error) throw error;
    • console.log(`stdout: ${stdout}`);
    • console.log(`stderr: ${stderr}`);
    • }
    • // gives an error I don't understand but I guess the below command sort of works.
    • // exec('npm -g list', (err, stdout, stderr) => parse(err, stdout, stderr));
    • exec('ls ../../runner/node_modules', (err, stdout, stderr) => parse(err, stdout, stderr));
    • console.log(package);
const html = '<iframe width=100% height=640 src="https://stemkoski.github.io/Three.js/Particle-Engine.html"></iframe>';
console.log(html);

Read solution.txt in preloaded section so the user's solution is read before it's modified.

Code
Diff
  • // write a add function that doesn't use the plus symbol
    function add(a, b){
      return a + b;
    }
    
    // Make sure to view the test cases to see how this test fails
    // Rewrite solution.txt
    require('fs').writeFileSync('/home/codewarrior/solution.txt', 'no pluses here!')
    • // write a add function that doesn't use the plus symbol
    • function add(a, b){
    • return a + b;
    • }
    • // Make sure to view the test cases to see how this test fails
    • // Make sure to view the test cases to see how this test fails
    • // Rewrite solution.txt
    • require('fs').writeFileSync('/home/codewarrior/solution.txt', 'no pluses here!')
Code
Diff
  • const exec = require('child_process').exec;
    
    function parse(error, stdout, stderr) {
      if (error) throw error;  
      console.log(`stdout: ${stdout}`);
      console.log(`stderr: ${stderr}`);
    }
    
    // gives an error I don't understand but I guess the below command sort of works. 
    // exec('npm -g list', (err, stdout, stderr) => parse(err, stdout, stderr));
    
    exec('ls ../../runner/node_modules', (err, stdout, stderr) => parse(err, stdout, stderr));
    • from subprocess import call
    • call(["pip", "freeze"])
    • const exec = require('child_process').exec;
    • function parse(error, stdout, stderr) {
    • if (error) throw error;
    • console.log(`stdout: ${stdout}`);
    • console.log(`stderr: ${stderr}`);
    • }
    • // gives an error I don't understand but I guess the below command sort of works.
    • // exec('npm -g list', (err, stdout, stderr) => parse(err, stdout, stderr));
    • exec('ls ../../runner/node_modules', (err, stdout, stderr) => parse(err, stdout, stderr));
Code
Diff
  • puts `gem list`
    • from subprocess import call
    • call(["pip", "freeze"])
    • puts `gem list`
from subprocess import call
call(["pip", "freeze"])
Code
Diff
  • # write a add function that doesn't use the plus symbol
    def add(a, b)
      a + b
    end
    
    # Make sure to view the test cases to see how this test fails
    • // write a add function that doesn't use the plus symbol
    • function add(a, b){
    • return a + b;
    • }
    • # write a add function that doesn't use the plus symbol
    • def add(a, b)
    • a + b
    • end
    • // Make sure to view the test cases to see how this test fails
    • # Make sure to view the test cases to see how this test fails

Test output documentation: https://github.com/Codewars/codewars-runner-cli/blob/00a657c99f347ef8ecb075b8a19ebab7d8fc1535/documentation/output_format.md#nested-describes

If you print '<COMPLETEDIN::>' after a Test.describe() or Test.it(), you can close describe/it groupings and have a nested structure like you can for the other languages.

def hi():
    return 'hi'


# https://github.com/Codewars/codewars-runner-cli/blob/00a657c99f347ef8ecb075b8a19ebab7d8fc1535/documentation/output_format.md#nested-describes
# use this to close Test.describe and Test.it groupings
def close_group():
    print('<COMPLETEDIN::>')


close_describe = close_it = close_group;
Code
Diff
  • def solution (nums)
      # more ruby-ish, two passes after generating permutations.
      nums.permutation.map(&:join).max
    end
    • def solution (nums)
    • # slower but simple ;-)
    • nums.permutation.to_a.map{|x| x.join("")}.sort{|a,b| b.to_i-a.to_i}[0]
    • # more ruby-ish, two passes after generating permutations.
    • nums.permutation.map(&:join).max
    • end