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 foo | |
puts "Foo!" | |
def bar | |
puts "Bar!" | |
end | |
end | |
begin | |
bar |
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 have_fun_refactoring_me | |
success = true | |
if giant_chunk_of_code_fails_somewhere? | |
success = false | |
end | |
rescue Exception => e | |
success | |
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
-- If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. -- | |
-- The sum of these multiples is 23. -- | |
-- Find the sum of all the multiples of 3 or 5 below 1000. -- | |
allNums :: Int -> [Int] | |
allNums max = [1..max] | |
divisible :: Int -> Int -> Bool | |
divisible x y = y `mod` x == 0 |
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
puts (1..999).select { |i| (i % 3).zero? || (i % 5).zero? }.reduce(:+) |
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 LCVariable < Struct.new(:name) | |
def replace(name, replacement) | |
if self.name == name | |
replacement | |
else | |
self | |
end | |
end | |
def callable? |
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
require 'benchmark' | |
def no_rescue | |
1 + 1 | |
end | |
def rescued | |
1 + 1 | |
rescue Exception | |
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
class LCVariable < Struct.new(:name) | |
def to_s | |
name.to_s | |
end | |
def inspect | |
to_s | |
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
module Enumerable | |
def compact_flat_map(&block) | |
return to_enum(:compact_flat_map) unless block_given? | |
compact.flat_map(&block) | |
end | |
alias_method :compact_collect_concat, :compact_flat_map | |
end | |
p [1, 2, nil, 4, 5, nil].compact_flat_map { |i| [i + 1, i - 1] } |
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 FARule < Struct.new(:state, :character, :next_state) | |
def applies_to?(state, character) | |
self.state == state && self.character == character | |
end | |
def follow | |
next_state | |
end | |
def inspect |
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
require 'treetop' | |
Treetop.load('simple') | |
parse_tree = SimpleParser.new.parse('while (x < 5) { x = x * 3 }') | |
statement = parse_tree.to_ast | |
puts "THE RESULT: #{statement.evaluate({ x: Number.new(1) })}" | |
puts | |
puts "TRANSLATED TO RUBY: #{statement.to_ruby}" |