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 Vehicle | |
class << self | |
attr_accessor :count | |
def finalize(id) | |
@count -= 1 | |
end | |
def all #returns an array of all Vehicle objects | |
ObjectSpace.each_object(Vehicle).to_a | |
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
# Functional: | |
new_person = ->(name,birthdate,gender,title,id=nil) { | |
return ->(attribute) { | |
return id if attribute == :id | |
return name if attribute == :name | |
return birthdate if attribute == :birthdate | |
return gender if attribute == :gender | |
return title if attribute == :title | |
nil |
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
# Firstly, install gems: 'evilr' and 'include' | |
# Example with User in a shop application. | |
# He has two roles | |
# - buyer (with money on the account and so on) | |
# - reviewer (he can review books he bought) | |
require 'evilr' | |
require 'include' |
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
params[:foo] = DecimalSupport.parse_model_decimals(params[:foo], Foo) |
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 before_save(model) | |
model.class.columns.each do |column| | |
if column.type == :decimal | |
int_part, frac_part = model.send(column.name).to_s.split(".") | |
new_value = [int_part, frac_part[0...column.scale]].join(".") | |
model.send("#{column.name}=", new_value) | |
end | |
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
1.9.3p362 :002 > [1,2,3,4,5].find_and_replace(:odd?, &:to_s) | |
=> ["1", 2, "3", 4, "5"] | |
1.9.3p362 :003 > [1,2,3,4,5].find_and_replace(Proc.new(&:odd?), &:to_s) | |
=> ["1", 2, "3", 4, "5"] | |
1.9.3p362 :005 > [1,2,3,4,5].find_and_replace(->(x) { x.odd? }, &:to_s) | |
=> ["1", 2, "3", 4, "5"] | |
1.9.3p362 :006 > [1,2,3,4,5].find_and_replace(->(x) { x.odd? }) do |x| | |
1.9.3p362 :007 > x.to_s | |
1.9.3p362 :008?> end | |
=> ["1", 2, "3", 4, "5"] |
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
type ast = Cons of ast * ast | |
| Write | |
| Read | |
open Printf | |
open Scanf | |
let myVar = ref 0 | |
let set_myVar x = |
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
~% echo "" > bar | |
~% cat bar | |
~% irb | |
1.9.3-p547 :001 > f = File.open("bar", "w") | |
=> #<File:bar> | |
1.9.3-p547 :002 > f.write("test1") | |
=> 5 | |
1.9.3-p547 :003 > | |
zsh: suspended irb |
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
t1 = Thread.new do | |
f = File.open("bar", "w") | |
sleep(rand(5) + 2) | |
f.write("test_1") | |
f.close | |
end | |
t2 = Thread.new do | |
f = File.open("bar", "w") |
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
str = "The quick fox and dog trololo" | |
/fox/.match(str) | |
def search2(str) | |
/and/.match(str) | |
puts "Inside method2: #{$&}" | |
end | |
def search(str) | |
/dog/.match(str) |
OlderNewer