#!/usr/bin/env ruby
class CLI < Thor
def search
# ...
selected = gets.chomp.to_i # this will raise: Broken pipe (Errno::EPIPE)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | |
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| body = %{"title": "Creating an awesome worklog client using Elixir"} |> Poison.encode! | |
| HTTPoison.post! @url, body, %{"Authorization" => "token your_access_token"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| d1 = Date.new 2016, 7, 1 | |
| d2 = Date.new 2016, 7, 31 | |
| (d1 .. d2).each { |d| puts d unless d.sunday? or d.saturday? } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # iterate through a date range, and print the dates | |
| date_range |> Enum.each(fn date -> date |> inspect |> IO.puts end) |
OlderNewer