Skip to content

Instantly share code, notes, and snippets.

import random
g = random.randrange(-10, 11, 1)
f = random.randrange(-10, 11, 1)
a = []
for i in range(0,10):
try:
x = int(input(" %d" %g + " + " + "%d = " %f))

DOM Manipulation/Parsing

Reading and Writing Element Attributes

Remember:

<element attribute="value">InnerHTML</element>

Example:

Comparisons Between Ruby and JavaScript

Iterating through over an array and printing each element

Ruby:

my_array = ["first_element", "second_element", "third_element"]

my_array.each do |element|
  puts element

Comparison Between AJAX and Ruby

Both of these pieces of code:

  • Make an HTTP GET request to the same URL
  • Parse the response (which is in JSON format) into datatypes (Hash for Ruby and Object for JavaScript)
  • Retrieve the results array and iterate over each Star Wars character

Ruby

{
"window.zoomLevel": 0,
"editor.minimap.enabled": false,
"editor.fontFamily": "'monospace'",
"editor.fontSize": 14,
"files.exclude": {
"**/node_modules": true
},
"files.defaultLanguage": "txt",
"workbench.startupEditor": "newUntitledFile",
<div class="btn-group mt-1 mx-auto">
<button id="gantt-day" type="button" class="gantt-period btn btn-sm btn-secondary">Day</button>
<button id="gantt-week" type="button" class="gantt-period btn btn-sm btn-secondary">Week</button>
<button id="gantt-month" type="button" class="gantt-period btn btn-sm btn-secondary">Month</button>
</div>
<script>
document.querySelectorAll(".gantt-period").forEach(function(button) {
button.addEventListener("click", function(event) {
railsGantt.gantt.change_view_mode(event.currentTarget.innerHTML);
#!/bin/sh
mkdir $1 && cd $1
yarn init
yarn add webpack webpack-dev-server \
babel-core babel-preset-es2015 \
babel-loader sass-loader css-loader style-loader \
extract-text-webpack-plugin \
node-sass --dev

Reading and Writing to CSV files in Ruby

These are the very basics of reading and writing to CSV files in Ruby. For more details and other available options consider checking out the full documentation: here


Consider a given csv file scientists.csv with the following content:

@Martin-Alexander
Martin-Alexander / fetch-all-repos.sh
Created June 27, 2018 16:33
Download & Setup All Projects
for project_dir in */; do
cd "$project_dir"
git checkout master
git checkout -- .
git pull
bundle install
yarn install
rails db:drop db:create db:migrate db:seed
cd ..
done
class Threater
def self.run(iterations = 1_000_000)
last_person_got_seat = 0.0
total_runs = 0.0
iterations.times do |i|
if i % 1000 == 0
print("\rNº of iterations: #{i.to_s.ljust(iterations.to_s.length)}")
end
last_person_got_seat += 1 if new.last_person_got_seat