Skip to content

Instantly share code, notes, and snippets.

View Jeweller-Tsai's full-sized avatar

nate Jeweller-Tsai

View GitHub Profile
@Jeweller-Tsai
Jeweller-Tsai / gist:2436935
Created April 21, 2012 12:45
How to take STDIN in the middle of a method execution?

How to take STDIN in the middle of a method execution?

 #!/usr/bin/env ruby

class CLI < Thor
  def search
    # ...

    selected = gets.chomp.to_i # this will raise: Broken pipe (Errno::EPIPE)
@Jeweller-Tsai
Jeweller-Tsai / download.rb
Created May 26, 2012 16:06
Using HTTP range to download a file separately
require "net/http"
require 'uri'
def download(uri_str)
uri = URI uri_str
Net::HTTP.new(uri.host, uri.port).start do |http|
head = http.request_head uri.path
length = head.content_length
puts length
require 'em-http-request'
require 'em-synchrony'
require 'em-synchrony/em-http'
module Gmusic
# = AsyncRequest
# This module is powered by {EventMachine}[https://github.com/eventmachine/eventmachine] and {em-http-request}[https://github.com/igrigorik/em-http-request] and {em-synchrony}[https://github.com/igrigorik/em-synchrony].
# By taking advantage of EventMacine, it allows you to issue parallel requests. You can easily control concurrency and follow redirects.
#
use std::io::{BufReader,BufRead};
use std::fs::File;
use std::time::Duration;
use std::thread;
use std::env;
fn main() {
fn tail(reader: &mut BufReader<&File>) {
let time = Duration::from_millis(10);
loop {
defmodule ElixirTail do
def tail path do
Stream.resource(fn -> File.open!(path) end,
fn file ->
case IO.read(file, :line) do
:eof ->
:timer.sleep 10
{[], file}
data ->
IO.write data
body = %{"title": "Creating an awesome worklog client using Elixir"} |> Poison.encode!
HTTPoison.post! @url, body, %{"Authorization" => "token your_access_token"}
d1 = Date.new 2016, 7, 1
d2 = Date.new 2016, 7, 31
(d1 .. d2).each { |d| puts d unless d.sunday? or d.saturday? }
def july do
1..31
|> Enum.map(fn i ->
{:ok, d} = Date.new 2016, 7, i
d
end)
|> Enum.filter(fn d ->
!weekend?(d)
end)
end
defmodule DateRange do
defstruct first: nil, last: nil
end
d1 = ~D[2016-07-01]
d2 = ~D[2016-07-31]
%DateRange{first: d1, last: d2} # => %DateRange{first: ~D[2016-07-01], last: ~D[2016-07-31]}
# iterate through a date range, and print the dates
date_range |> Enum.each(fn date -> date |> inspect |> IO.puts end)