Skip to content

Instantly share code, notes, and snippets.

@NIA
NIA / Main.scala
Last active December 25, 2015 01:39
Getting time diffs using https://github.com/scalaj/scalaj-time (JodaTime wrapper for scala)
// locate this in src/main/scala/ to compile with sbt
import org.joda.time.Period
import org.joda.time.format.PeriodFormat
import org.scala_tools.time.Imports._
object Main extends App {
def periodDiff(t1: Period)(t2: Period) = PeriodFormat.getDefault print (t1 - t2).normalizedStandard
println( periodDiff(2.hours + 14.minutes + 39.seconds)(1.hour + 32.minutes + 43.seconds) )
@NIA
NIA / gist:5662466
Last active December 17, 2015 19:39
Наброски по Чёрным дырам
\url{http://www.krugosvet.ru/enc/nauka_i_tehnika/astronomiya/CHERNAYA_DIRA.html}
ЧЕРНАЯ ДЫРА – область пространства, в которой гравитационное притяжение настолько сильно, что ни вещество,
ни излучение не могут эту область покинуть. Для находящихся там тел вторая космическая скорость (скорость убегания)
должна была бы превышать скорость света, что невозможно, поскольку ни вещество, ни излучение не могут двигаться быстрее
света. Поэтому из черной дыры ничто не может вылететь. Границу области, за которую не выходит свет, называют
«горизонтом событий», или просто «горизонтом» черной дыры.
\section{История}
Основные этапы \url{https://ru.wikipedia.org/wiki/Чёрная_дыра}
@NIA
NIA / gist:4959907
Created February 15, 2013 11:43
Object#inspect demo
irb> class B
irb> def initialize
irb> @a = 1
irb> @b = 2
irb> end
irb> end
=> nil
irb> B.new.to_s
=> "#<B:0x4592ca0>"
irb> B.new.inspect
@NIA
NIA / qextserial_example.cpp
Created February 10, 2013 18:21
Example client/server using QextSerialPort
#include "widget.h"
#include "ui_widget.h"
#include "qextserialport.h"
#include <QInputDialog>
namespace {
const QString DEFAULT_PORT = "COM6";
}
Widget::Widget(QWidget *parent) :
@NIA
NIA / tricks
Created February 1, 2013 13:16 — forked from cypok/tricks
// Paste following into the address bar.
// simple notepad
data:text/html, <html contenteditable>
// edit any text on web page
javascript:document.getElementsByTagName('html')[0].contentEditable=true;
// powerful notepad for Ruby or any other language
// https://gist.github.com/4670615
@NIA
NIA / gist:4605018
Last active December 11, 2015 13:08
resources :doctors do
get 'new_schedule', :on => :member
end
resources :schedules do
get 'new_appointment', :on => :member
end
resources :appointments
# With this /doctors/123/new_schedule will map to DoctorsController#new_schedule
# and /schedules/321/new_appointment will map to SchedulesController#new_appointment
# First I modify your models a bit:
Class Appointment ...
# belongs_to :doctor (replace it with this ↓)
has_one :doctor, through: :schedule # This means 'doctor_id' is not stored in appointments table, it is got from schedule via join
belongs_to :schedule
end
Class Schedule ...
belongs_to :doctor
@NIA
NIA / js-getters.rb
Created January 23, 2013 08:21
Javascript-like working with Hash in Ruby (HACK)
# Add javascript-like functionality to Hash:
# write hash.prop instead of hash['prop']
#
# NB: Your keys must NOT be named like standard methods of Hash
# In such case, use normal notation, e.g. hash['key']
#
# So it is kinda dangerous hack!
class Hash
def method_missing(meth, *args)
method = meth.to_s # Symbol -> String
@NIA
NIA / conditions.rb
Last active December 11, 2015 11:48
Illustration of using tail conditions for http://stackoverflow.com/a/14460975/693538 SO answer
# Check if it is present...
if row[1].present?
# If it is present:
# Check if it is a subclass of numeric...
if row[1].is_a? Numeric
# If it is present AND it is a number:
oem = row[1].to_i.to_s # <- Convert it to string
else
# If it is present BUT is not a number:
oem = row[1] # ...then it is string, conversion not required
@NIA
NIA / collections-1.scala
Last active December 10, 2015 16:28
Very laconic functional-style working with collections in Scala
// Example 1: join two collections of filenames, find existing ones and load them
(configFiles ++ extraConfigFiles) map { new File(_) } filter { _.canRead } foreach load
// Example 2: invoke `prompt` function in an infinite loop, transform the result with trim method, filter out non-empty and process the others
Iterator continually(prompt) map { _.trim } filterNot { _.isEmpty } foreach { input => ... }