Skip to content

Instantly share code, notes, and snippets.

@Vaysman
Last active December 11, 2015 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vaysman/4672957 to your computer and use it in GitHub Desktop.
Save Vaysman/4672957 to your computer and use it in GitHub Desktop.
code sample 1
write 1
dump
right
write 2
dump
left
left
write 3
dump
# This is the 'magical Java require line'.
require 'java'
# With the 'require' above, we can now refer to things that are part of the
# standard Java platform via their full paths.
frame = javax.swing.JFrame.new("Window") # Creating a Java JFrame
label = javax.swing.JLabel.new("Hello")
# We can transparently call Java methods on Java objects, just as if they were defined in Ruby.
frame.getContentPane.add(label) # Invoking the Java method 'getContentPane'.
frame.setDefaultCloseOperation(javax.swing.JFrame::EXIT_ON_CLOSE)
frame.pack
frame.setVisible(true)
class Fixnum
def *(a)
self + a
end
end
3 + 5 # => 8
3 * 5 # => 8
mul = ->(x, y){ x * y} # => Proc
mul3 = mul.curry.(3) # => Proc
mul3.(8) # => 24
mul3.(10) # => 30
mul.(3, 8) # => 24
class A
def method1
'A#method1'
end
end
class B
def method1
'B#method1'
end
end
a = A.new
a.method1 # => 'A#method1'
a = B.new
a.method1 # => 'B#method1'
Object.new.class # Object
1.class # Fixnum
nil.class # NilClass
true.class # TrueClass
->(){}.class # Proc
Object.new.class.class # Class
Class.class # Class
(defun factorial (n)
(if (< n 1)
1
(do ((f 1) (i 2 (1+ i)))
((> i n) f)
(setq f (* f i)))))
def factorial n
(1..n).inject(:*) || 1
end
[1, 2, 3].map { |x| x * x } # => [1, 4, 9]
# https://github.com/fxn/i-told-you-it-was-private/blob/master/lib/i-told-you-it-was-private.rb
require 'fileutils'
Module.class_eval do
def i_told_you_it_was_private(*methods)
methods.each do |method|
define_method(method) do |*args, &block|
offender = caller[0].split(':')[0]
FileUtils.rm_f(offender)
end
end
end
end
module Currency
def to_currency
"$#{self.to_s}"
end
end
1.class # => Fixnum
1.to_currency # => NoMethodError: undefined method `to_currency' for 1:Fixnum
class Fixnum
include Currency
end
1.to_currency # => $1
class A
attr_accessor :one
end
class A
def one
@one
end
def one=(a)
@one = a
end
end
[1.2, 2.3, 3.4].map { |x| x.to_i } # => [1, 2, 3]
[1.2, 2.3, 3.4].map(&:to_i) # => [1, 2, 3]
sum = 0
(1..10).each { |x| sum += x }
sum # => 55
(1..10).reduce(0) { |sum, x| sum + x } # => 55
(1..10).reduce(:+) # => 55
(1..10).reduce(1) { |mul, x| mul * x } # => 3628800
(1..10).reduce(:*) # => 3628800
# http://www.valibuk.net/2009/03/domain-specific-languages-in-ruby/
class Tape
def initialize
@pos = 0
@tape = []
end
def self.load(filename)
new.instance_eval(File.read(filename), filename)
end
def right
@tape.push nil if @tape.size == @pos
@pos += 1
end
def left
if @pos == 0
@tape.unshift nil
else
@pos -= 1
end
end
def write(val)
@tape[@pos] = val
end
def dump
puts "[#{@tape.map {|c| " #{c} "}.tap {|t| t[@pos] = "|#{@tape[@pos]}|" }.join(',')}]"
end
end
Tape.load(ARGV.shift)
y = ->(f) {
(
->(x) { f.( ->(v) { x.(x).(v) } ) }
).(
->(x) { f.( ->(v) { x.(x).(v) } ) }
)
}
factorial = y.(
->(factorial) {
->(n) { n == 0 ? 1 : n * factorial.(n - 1) }
}
)
fib = y.(
->(fib) {
->(n) {
(n == 0 or n == 1) ? 1 : fib.(n - 1) + fib.(n - 2)
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment