Skip to content

Instantly share code, notes, and snippets.

@NIA
NIA / arrows.scala
Last active December 14, 2015 11:10
Scala hieroglyph experiments
import language.{postfixOps,implicitConversions}
case class -->[A, B] (from: A, to: B)
implicit class ArrowSugar[A](private val self: A) { def --> [B] (other: B) = new -->(self, other)}
def diff(arrow: Int-->Int) = arrow match {
case x --> y => y - x
}
diff(1 --> 4) should be (3)
@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 => ... }
@NIA
NIA / newyear2013.rb
Last active December 10, 2015 10:08
A funny function that congratulates everybody with 'happy new 1357002013'th UNIX timestamp'... well, in Russian.
# encoding: utf-8
#
# Prerequisites:
# sudo gem install colorize
#
# Example usage:
# $ irb -r './newyear2013.rb'
# irb(main):001:0> congratulate! :everybody
#
require 'colorize'