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' | |
| require 'open-uri' | |
| module Scrape | |
| def self.links site | |
| page = Nokogiri::HTML(open site) | |
| @links = [] | |
| page.search('link').each do |link| | |
| @links << link.attributes['href'].value | |
| 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 Chromosome | |
| attr_accessor :bit_string, :fitness | |
| def initialize length, bit_string = nil | |
| @length = length | |
| if bit_string.nil? | |
| generate_random length | |
| else | |
| @bit_string = bit_string |
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 'ohm' | |
| module Om | |
| class << self | |
| def connect | |
| Ohm.connect | |
| end | |
| def [] key | |
| Marshal.load(Ohm.redis.get key) |
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 'celluloid' | |
| class Clock | |
| include Celluloid | |
| def initialize | |
| @ticker = %w[tick tock].cycle | |
| end | |
| def wind |
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
| #!/usr/bin/env ruby | |
| require 'benchmark' | |
| def run str, bench | |
| bench.report "#{str.length + 1} chars" do | |
| 1_000_000.times do | |
| new_string = str + 'x' | |
| 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
| for i in [1..100] | |
| console.log(['Fizz' if i % 3 is 0] + ['Buzz' if i % 5 is 0] or i) |
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 Float | |
| def round(ndigits=0) | |
| ndigits = Rubinius::Type.coerce_to(ndigits, Integer, :to_int) | |
| if ndigits == 0 | |
| return Rubinius.invoke_primitive :float_round, self | |
| elsif ndigits < 0 | |
| return truncate.round ndigits | |
| 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
| # Code inside a 'Module' is organized into 'methods'. | |
| # Modules are named with CamelCase and methods are named with snake_case. | |
| module ModulesAreCamelCase # This is a Module. | |
| # The Module's methods' go here. | |
| end | |
| module ModulesAreCamelCase | |
| def self.methods_are_snake_case # This is a method. |
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
| module Fib | |
| def self.show this_many # this_many is an arguement. | |
| puts this_many # arguements are available inside the method | |
| end | |
| end | |
| Fib.show # If Ruby expects a method to have an arguement, you'd better provide. | |
| # ArgumentError: wrong number of arguments (0 for 1) | |
| Fib.show 5 |
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
| >> Benchmark.measure do | |
| | 10_000.times { "aoiudfudasf\nasoidufasjdfads\n".split("\n").reverse_each.first } | |
| | end | |
| => 0.020000 0.000000 0.020000 ( 0.020835) | |
| >> Benchmark.measure do | |
| | 10_000.times { "aoiudfudasf\nasoidufasjdfads\n"[/(.*)\Z/] } | |
| | end | |
| => 0.030000 0.000000 0.030000 ( 0.030893) |