Skip to content

Instantly share code, notes, and snippets.

View therod's full-sized avatar

Rodrigo therod

View GitHub Profile

Ruby Cheatsheet

Basic Math Operations

5.4 - 2.2 # Subtraction
4 * 72 # Multiplication
7 / 3.5 # Division
3 ** 7  # Exponentiation
class Person
attr_accessor :first_name, :last_name, :birth_year, :car_registration
def initialize(first_name, last_name, birth_year, car_registration)
@first_name = PersonDataChecker.check_name(first_name)
@last_name = PersonDataChecker.check_name(last_name)
@birth_year = PersonDataChecker.check_birth_year(birth_year)
@car_registration = PersonDataChecker.check_car_registration(car_registration)
end
end
! Disable everything but the search & video player on YouTube.
www.youtube.com###primary > .ytd-two-column-browse-results-renderer.style-scope
www.youtube.com###secondary
www.youtube.com###guide-inner-content
www.youtube.com##ytd-mini-guide-renderer
www.youtube.com##ytd-watch-next-secondary-results-renderer

Smalltalk Best Practice Patterns in Ruby

1 – INTRODUCTION

  • We aren’t always good at guessing where responsibilities should go. Coding is where our design guesses are tested. Being prepared to be flexible about making design changes during coding results in programs that get better and better over time.

  • If you’re programming along, doing nicely, and all of a sudden your program gets balky, makes things hard for you, it’s talking. It’s telling you there is something important missing.

  • Some of the biggest improvements come from figuring out how to eliminate:

  • Duplicate code (even little bits of it)
#!/bin/bash
## Remove all local timemachine backups
for i in $(tmutil listlocalsnapshotdates / | awk '{print $1}' | sed -n '1!p' | xargs echo); do tmutil deletelocalsnapshots $i; done
mentors = ["Hans", "Ruedi"].shuffle
attendees = ["Klara", "Kevin", "Mike", "Sandro", "Sandy", "Silvan", "Lala"].shuffle
number_of_groups = (attendees.count / mentors.count)
groups = attendees.each_slice(number_of_groups).to_a
mentors.each_with_index do |mentor, index|
puts "Group #{index + 1}: #{mentor}"
puts "===================="
puts groups.shift
require 'pry'
keys = `aws s3api list-objects --bucket master21 --query 'Contents[].{Key: Key}' --output text | grep -v 'folder'`
keys = keys.split("\n")
keys.each do |key|
system("aws s3api put-object-acl --acl bucket-owner-full-control --bucket master21 --key '#{key}'")
end
require 'open-uri'
require 'prawn'
class OrangeTree
def initialize
@height = 0
@orange_count = 0
@fruit = 0
@age = 0
puts "Ha! Did you really think I would give away the solution that easily?"
puts "Try again!"
class OrangeTree
def initialize
@height = 0
@age = 0
@oranges = 0
@alive = true
end
def one_year_passes
@alive = false if @height > 10 && rand(2) == 1