Skip to content

Instantly share code, notes, and snippets.

View namtx's full-sized avatar
🐤
Fullstuck developer

0x6e616d7478 namtx

🐤
Fullstuck developer
View GitHub Profile

problems

class Address < ActiveRecord::Base
  belongs_to :customer
end

class Customer <ActiveRecord::Base
  has_one :address
  has_many :invoices
end
@namtx
namtx / presenters.md
Created June 15, 2017 08:26 — forked from somebox/presenters.md
Thoughts About Rails Presenters

Thoughts about Rails Presenters

This is a collection of links, examples and rants about Presenters/Decorators in Rails.


The "Decorator" pattern slowly started gaining popularity in Rails several years ago. It is not part of core Rails, and there's many different interpretations about how it should work in practice.

Jay Fields wrote about it in 2007 (before he switched back to Java and then Clojure): http://blog.jayfields.com/2007/03/rails-presenter-pattern.html

@namtx
namtx / postgres-cheatsheet.md
Created September 22, 2017 02:13 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

Some interesting flags (to see all, use -h):

  • -E: will describe the underlaying queries of the \ commands (cool for learning!)
  • -l: psql will list all databases and then exit (useful if the user you connect with doesn't has a default database, like at AWS RDS)
![lgtm](http://i.imgur.com/XTVGHwg.gif)
class Quesion < ApplicationRecord
SUBMITITABLE_TYPES = %w(Open MultipleChoice Scale).freeze
validates :maximum, presence: true, if: :scale?
validates :minimum, presence: true, if: :scale?
validates :question_type, presence: true, inclusion: SUBMITITABLE_TYPES
validates :title, presence: true
def summary
case question_type
@namtx
namtx / greeting.rb
Last active May 17, 2018 08:54
Ruby Metaprogramming
class Greeting
def initialize text
@text = text
end
def welcome
@text
end
end
@namtx
namtx / encrypter.rb
Last active June 7, 2018 02:02
adapter
class Encrypter
def initialize(key)
@key = key
end
def encrypt(reader, writer)
key_index = 0
while not reader.eof?
clear_char = reader.getc
encrypted_char = clear_char ^ @key[key_index]
writer.putc(encrypted_char)
@namtx
namtx / my_log_subscriber.rb
Created June 12, 2018 06:01
How to use ActiveSupport::LogSubscriber
require 'active_support/log_subscriber'
module ActiveStorage
class LogSubscriber < ActiveSupport::LogSubscriber
def service_upload(event)
message = event.payload[:something]
info event, color(message, GREEN)
end
end
end
@namtx
namtx / App.jsx
Created July 7, 2018 07:56
React `ref` with `HOC`
import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";
import InputField from "./FancyInput";
class App extends Component {
constructor(props) {
super(props);
this.textInput = React.createRef();
// this.handleSubmit = this.handleSubmit.bind(this);

Content: HuyND đã lập trình một con robot. Robot đi theo đường thẳng và có thể nhận lệnh "T" ("quay 180 độ") và "F" ("đi 1 đơn vị về phía trước").

Bạn được cho 1 danh sách các lệnh cho robot. Bạn cần phải đổ chính xác n lệnh từ danh sách (1 lệnh có thể đổi nhiều lần). Vậy Robot có thể đi tối đa bao xa nếu theo đúng thứ tự các lệnh sau khi đã chỉnh sửa ?

Input

Dòng đầu tiên là chuỗi các dòng lệnh ban đầu. Chiều dài từ 1 đến 100 kí tự, bao gồm chỉ 2 lệnh "T" và "F". Dòng thứ 2 là số nguyên n (1 ≤ n ≤ 50) — Số lệnh bạn phải đổi trong danh sách.