This file contains 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 UIView | |
def top | |
frame.origin.y | |
end | |
def left | |
frame.origin.x | |
end | |
def height | |
frame.size.height | |
end |
This file contains 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
% A brute force algorithm to solve the Surpassing Problem posed by Martin Rem in 1988 | |
% | |
% By definition, a surpasser of an element of an array is a greater element to the right, | |
% so x[j] is a surpasser of x[i] if i < j and x[i] < x[j]. The surpasser count of an element | |
% is the number of its surpassers. For example, the surpasser count of the string GENERATING | |
% are given by | |
% | |
% G E N E R A T I N G | |
% 5 6 2 5 1 4 0 I 0 0 | |
% The maximum surpasser count is 6. The first occurrence of the letter E has six surpassers, |
This file contains 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
# Ruby Implementation of lazy array for memoized fib | |
# Inspired by Pat Shaughnessy's talk at http://bostonrb.org/presentations/month/March-2013 | |
# Define a LazyArray class that caches results | |
# The cache is valid as long as we use it functionally meaning that | |
# the fn has no side effects and always returns the same output for the same input | |
class LazyArray | |
def initialize(fn) | |
@fn = fn | |
@cache = {} |
This file contains 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 fib(x) | |
puts "fib(#{x})" | |
return 1 if [0,1].include? x | |
return fib(x-1) + fib(x-2) | |
end | |
cache = (0..Float::INFINITY).lazy.map {|x| fib(x) } | |
class << cache | |
def [](n) | |
take(n).force.last |
This file contains 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
#! /usr/bin/env ruby | |
Dir['/srv/*/current'].each do |dir| | |
if File.exist?("#{dir}/Gemfile.lock") | |
puts dir | |
puts `cd #{dir} && bundle-audit` | |
end | |
end | |
This file contains 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
# ============================= | |
# Test case showing what we expect to happen | |
a1 = 'z' | |
b1 = 'z' | |
a = [a1] | |
b = [b1] | |
# array '-' https://github.com/ruby/ruby/blob/trunk/array.c#L3806 | |
# It compares elements using their #hash and #eql? methods for efficiency. |
This file contains 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
<% sections.each do |section| %> | |
<li class='<%="active" if active_section? section%>'> | |
<% = link_to section %> | |
<% end %> |
This file contains 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
module Sth | |
module Generators | |
class ConfigGenerator < ::Rails::Generators::Base | |
TXT = '# some inserted text' | |
desc 'Configs application controller' | |
def edit_application_Controller | |
if File.exist?("app/controllers/application_controller.rb") | |
insert_into_file "app/controllers/application_controller.rb", TXT, :after => "protect_from_forgery" | |
else |
This file contains 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 MyController < UIViewController | |
def viewDidLoad | |
super | |
top_view.addSubview chart_view([[0,0], [100,100], | |
[10, 20, 20, 15, 35], | |
["#203a65", "#37537c", "#5b7faa", "#aec2d6", "#aec2d6", "#ffffff"] | |
) | |
end | |
def chart_view(frame, data, colors) |
This file contains 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
# We crash with this definition | |
# Could the superclass be freed by RubyMotion's automatic memory management??? | |
class Person < Struct.new(:name) | |
end | |
# # Everything is fine if we define Person without Struct.new | |
# class Person | |
# attr_reader :name | |
# def initialize(attributes) | |
# @name = attributes[:name] |