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 'json' | |
| def d2j(data) | |
| case data | |
| when Hash | |
| if data.keys.length == 1 && %w[S N M L NULL BOOL].include?(data.keys.first) | |
| key, value = data.first | |
| case key | |
| when 'S', 'N' | |
| value.is_a?(String) && key == 'N' ? value.to_i : value |
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
| class Array | |
| def bsearch(e, l = 0, u = length - 1) | |
| if l <= u | |
| m = (l + u)/2 | |
| e < self[m] ? u = m - 1 : l = m + 1 | |
| e == self[m] ? m : bsearch(e, l, u) | |
| end | |
| 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
| require 'nokogiri' | |
| xml = <<EOS | |
| <html> | |
| <body> | |
| <h1><a href="#">This is title</a></h1> | |
| <p> | |
| Hello <i>world</i>! | |
| This is <em>another</em> line. | |
| <p><h3>And a paragraph <em>with</em> a heading.</h3></p> |
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
| <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
| <xsl:output method="xml" encoding="ISO-8859-1"/> | |
| <xsl:param name="indent-increment" select="' '"/> | |
| <xsl:template name="newline"> | |
| <xsl:text disable-output-escaping="yes"> | |
| </xsl:text> | |
| </xsl:template> | |
| <xsl:template match="comment() | processing-instruction()"> |
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
| # encoding: UTF-8 | |
| class HolidayInfo | |
| def initialize date | |
| @date = date | |
| @is_national = false | |
| @name = "" | |
| @is_substitute = false | |
| end | |
| attr_accessor :date, :is_national, :name, :is_substitute |
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 ugly_guess_jp str | |
| %w[UTF-8 EUC-JP Shift_JIS eucJP-ms Windows-31J].find do |encoding| | |
| begin | |
| tested = str.force_encoding(encoding) | |
| tested.valid_encoding? && tested.encode('UTF-8').valid_encoding? | |
| rescue Encoding::UndefinedConversionError => e | |
| nil | |
| end | |
| 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
| class BootstrapLinkRenderer < WillPaginate::LinkRenderer | |
| def to_html | |
| links = @options[:page_links] ? windowed_links : [] | |
| links.unshift(page_link_or_span(@collection.previous_page, 'prev', @options[:previous_label])) | |
| links.push(page_link_or_span(@collection.next_page, 'next', @options[:next_label])) | |
| html = @template.content_tag(:ul, links.join(@options[:separator])) | |
| @options[:container] ? @template.content_tag(:div, html, html_attributes) : html |
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
| aj = [[0,1,2],[2,0,1],[1,2,0]] | |
| n = 100 | |
| m = 10 | |
| while (n > 0) | |
| hm = m/2 | |
| (1..(m-6)/4).each { |a| a3 = a**3 | |
| ((a+1)..(((m2=m-a)-3)/3)).each { |b| b3 = b**3 | |
| # a+b+c+d == m; a+d == b+c (mod 6) => c == m/2-b (mod 3) | |
| c = b+1; c += aj[c%3][(hm-b)%3] | |
| upper_c = ((m3=m2-b)-1)/2 |
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
| -- Ramanujan Numbers: | |
| -- c.f. http://www.ipl.t.u-tokyo.ac.jp/~onoue/pub/jssst01.pdf | |
| module Main where | |
| main = print (take 10 (ramanujan (\(a,b) -> a^3 + b^3))) | |
| ramanujan f = [(x,y) | (x,y)<- zip s (tail s), f x == f y] where s = fsort f 1 | |
| fsort f k = (k,k) : fmerge f [(k,b) | b<-[k+1..]] (fsort f (k+1)) | |
| fmerge f (x:xs) (y:ys) | |
| | f x <= f y = x : fmerge f xs (y:ys) | |
| | otherwise = y : fmerge f (x:xs) ys |
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 'curb' | |
| require 'net/http' | |
| require 'open-uri' | |
| require 'benchmark' | |
| Net::HTTP.version_1_2 | |
| n = 100 | |
| Benchmark.bm do |x| |
NewerOlder