├── .gitignore ├── netlify.toml ├── docs ├── README.md ├── .vuepress │ ├── override.styl │ └── config.js ├── ranges.md ├── conditions.md ├── integers.md ├── variables.md ├── strings.md ├── arrays.md └── others.md ├── README.md └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /netlify.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | command = "npm run docs:build" 3 | publish = "docs/.vuepress/dist/" 4 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # ruby-codegolf 2 | 3 | You can find here some tips to play code golf with Ruby 4 | hugolgst 5 | -------------------------------------------------------------------------------- /docs/.vuepress/override.styl: -------------------------------------------------------------------------------- 1 | // showing default values 2 | $accentColor = #ce1e39 3 | $textColor = #2c3e50 4 | $borderColor = #eaecef 5 | $codeBgColor = #282c34 6 | -------------------------------------------------------------------------------- /docs/ranges.md: -------------------------------------------------------------------------------- 1 | # Ranges 2 | 3 | Item is member of a range 4 | 5 | ```ruby 6 | (0..10).member?(3) 7 | 8 | (0..10)===3 #Better 9 | ``` 10 | 11 | Range to array 12 | 13 | ```ruby 14 | ("a".."z").to_a 15 | 16 | [*?a..?z] #Better 17 | ``` 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ruby-codegolf [![Netlify Status](https://api.netlify.com/api/v1/badges/b5e48fbb-9696-4acb-9e87-83988c41ee5c/deploy-status)](https://app.netlify.com/sites/ruby-codegolf/deploys) 2 | 3 | [The documenation](https://ruby-codegolf.netlify.com/) 4 | -------------------------------------------------------------------------------- /docs/conditions.md: -------------------------------------------------------------------------------- 1 | # Conditions 2 | 3 | Condition shortcut 4 | 5 | ```ruby 6 | a=true 7 | if a 8 | puts "Yes it's true." 9 | end 10 | 11 | a=true 12 | puts "Yes it's true." if a #Better 13 | 14 | a=true 15 | a&&puts "Yes it's true." #The best 16 | ``` 17 | -------------------------------------------------------------------------------- /docs/integers.md: -------------------------------------------------------------------------------- 1 | # Integers 2 | 3 | Number shortcut 4 | 5 | ```ruby 6 | a=10000 7 | 8 | a=1e4 #Better 9 | ``` 10 | 11 | Print 12 | 13 | ```ruby 14 | puts 547 15 | 16 | p 547 #Better 17 | ``` 18 | 19 | -1 20 | 21 | ```ruby 22 | a=5 23 | (a-1) * 4 24 | 25 | ~-a * 4 #Better 26 | ``` 27 | -------------------------------------------------------------------------------- /docs/variables.md: -------------------------------------------------------------------------------- 1 | # Variables 2 | 3 | Define variables with one letter 4 | 5 | ```ruby 6 | sentence="This is a good sentence." 7 | s="This is a good sentence." #Better 8 | ``` 9 | 10 | Multiple variable assignement 11 | 12 | ```ruby 13 | a=2017 14 | b=50 15 | c="Fish" 16 | 17 | a,b,c=2017,50,"Fish" #Better 18 | ``` 19 | 20 | Puts variables 21 | 22 | ```ruby 23 | a="hugol" 24 | puts "My name is #{a}" 25 | 26 | $a="hugol" 27 | puts "My name is #$a" #Better 28 | ``` 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ruby-codegolf", 3 | "version": "1.0.0", 4 | "description": "Some tips for golfing in Ruby", 5 | "main": "index.js", 6 | "directories": { 7 | "doc": "docs" 8 | }, 9 | "scripts": { 10 | "docs:dev": "vuepress dev docs", 11 | "docs:build": "vuepress build docs" 12 | }, 13 | "devDependencies": { 14 | "vuepress": "^0.14.11" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | hugolgst 19 | }, 20 | hugolgst 21 | "license": "MIT", 22 | "bugs": { 23 | hugolgst 24 | }, 25 | hugolgst 26 | } 27 | -------------------------------------------------------------------------------- /docs/strings.md: -------------------------------------------------------------------------------- 1 | # Strings 2 | 3 | String match 4 | 5 | ```ruby 6 | a="Hello" 7 | if a.match /[a-zA-Z]/ 8 | puts "It's a match!" 9 | end 10 | 11 | a="Hello" 12 | if a=~/[a-zA-Z]/ 13 | puts "It's a match!" #Better 14 | end 15 | ``` 16 | 17 | String repetition 18 | 19 | ```ruby 20 | 6.times do 21 | print "Hello" 22 | end 23 | 24 | print "Hello"*6 #Better 25 | ``` 26 | 27 | Single char 28 | 29 | ```ruby 30 | puts "A" 31 | 32 | puts ?A #Better 33 | ``` 34 | 35 | Char ascii 36 | 37 | ```ruby 38 | puts 70.chr 39 | 40 | putc 70 #Better 41 | ``` 42 | 43 | \n shortcut 44 | 45 | ```ruby 46 | a=gets.split "\n" 47 | 48 | a=gets.split $/ #Better 49 | ``` 50 | -------------------------------------------------------------------------------- /docs/.vuepress/config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | title: 'ruby-codegolf', 3 | description: 'Some tips for golfing in Ruby', 4 | head: [ 5 | ['meta', { name: 'og:type', content: 'website' }], 6 | ['meta', { name: 'og:title', content: 'Ruby code golf tips' }], 7 | ['meta', { name: 'og:description', content: 'Code golf tips - Some tips for golfing in Ruby' }], 8 | ['meta', { name: 'theme-color', content: '#e53033' }] 9 | ], 10 | themeConfig: { 11 | nav: [ 12 | hugolgst 13 | ], 14 | sidebar: [ 15 | '/variables', 16 | '/strings', 17 | '/arrays', 18 | '/ranges', 19 | '/integers', 20 | '/conditions', 21 | '/others' 22 | ] 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /docs/arrays.md: -------------------------------------------------------------------------------- 1 | # Arrays 2 | 3 | Array assignement 4 | 5 | ```ruby 6 | a=["Hello", "this", "is", "an", "array"] 7 | 8 | a=%w(Hello this is an array) #Better 9 | ``` 10 | 11 | Array join 12 | 13 | ```ruby 14 | a=[1, 2, 3] 15 | puts a.join("+") 16 | 17 | a=[1, 2, 3] 18 | puts a*?+ #Better 19 | ``` 20 | 21 | Try to set your array as `$*` 22 | 23 | ```ruby 24 | a=[] 25 | 6.times do |i| 26 | a << i 27 | end 28 | 29 | 6.times do |i| 30 | $* << i 31 | end 32 | ``` 33 | 34 | Array shift 35 | 36 | ```ruby 37 | a=[1,2,3] 38 | a.shift 39 | 40 | a=[1,2,3] 41 | a[1..-1] # Better 42 | ``` 43 | 44 | Change compact 45 | 46 | ```ruby 47 | a=[1, nil, 2, nil, nil, 3] 48 | a.compact 49 | 50 | a-[nil] #Better 51 | ``` 52 | 53 | Uniq 54 | 55 | ```ruby 56 | a.uniq 57 | 58 | a|[] 59 | ``` 60 | -------------------------------------------------------------------------------- /docs/others.md: -------------------------------------------------------------------------------- 1 | # Others 2 | 3 | Shortcut for print 4 | 5 | ```ruby 6 | print "hello" 7 | 8 | $><<"hello" #Better 9 | ``` 10 | 11 | Think to remove `do end` block 12 | 13 | ```ruby 14 | 6.times do |i| 15 | puts i 16 | end 17 | 18 | 6.times{|i|puts i} #Better 19 | ``` 20 | 21 | You can change `each` to `map` 22 | 23 | ```ruby 24 | (0..9).to_a.each do 25 | puts "Hello" 26 | end 27 | 28 | (0..9).to_a.map do 29 | puts "Hello" #Better 30 | end 31 | ``` 32 | 33 | You can replace your last gets by `$_` 34 | 35 | ```ruby 36 | a=gets 37 | puts a 38 | 39 | gets 40 | puts $_ 41 | ``` 42 | 43 | Methods 44 | 45 | ```ruby 46 | def a n 47 | n*n 48 | end 49 | 50 | a=->n{n*n} # Better 51 | ``` 52 | 53 | Write n times w 54 | 55 | ```ruby 56 | 6.times do 57 | puts "Hello world" 58 | end 59 | 60 | puts ["Hello world"] * 6 #Better 61 | ``` 62 | --------------------------------------------------------------------------------