Skip to content

Instantly share code, notes, and snippets.

@zmalltalker
Created March 10, 2009 12:02
Show Gist options
  • Select an option

  • Save zmalltalker/76872 to your computer and use it in GitHub Desktop.

Select an option

Save zmalltalker/76872 to your computer and use it in GitHub Desktop.
== overview:
builtin: rake, rubygems, json, securerandom, HMAC digests
removed: base64 (unpack("m*"))
== nye ting:
Hash syntax ( {foo: "bar"} #=> {:foo => "bar"})
proc/lamnda syntax:
p = ->(n){ n + 1 }; p.(42) #=> 43
>> greeter = ->person{ "Hello there, #{person}!" }
=> #<Proc:0x3f7578@(irb):51 (lambda)>
>> greeter.call("Marius")
=> "Hello there, Marius!"
>> greeter.yield "Marius"
=> "Hello there, Marius!"
Flere Hash Enumerable operasjoner returnerer en hash (#select etc)
Hash ordered by insertion
Object#tap:
>> foo_with_thing = Foo.new.tap do |fooer|
?> fooer.bar = "it's where you get drinks"
>> end
=> #<Foo:0x3f5e30 @bar="it's where you get drinks">
(Object#returning i Rails/ActiveSupport)
Symboler mere som strenger:
:foo =~ /oo$/
String#[]:
1.8:
>> "foo"[0]
=> 102
1.9:
>> "foo"[0]
=> "f"
>> "foo"[0].ord
=> 102
String er ikke Enumerable:
>> "ruby".each
NoMethodError: undefined method `each' for "ruby":String
from (irb):38
from /usr/local/bin/irb1.9:12:in `<main>'
>> "ruby".each_char{|c| p c }
"r"
"u"
"b"
"y"
#each => #each_line
>> "ru\nby".each_line{|line| p line }
"ru\n"
"by"
Array/Hash#to_s == #inspect
>> [1,2,3].to_s
=> "[1, 2, 3]"
>> {:foo => 1, :bar => 2}.to_s
=> "{:foo=>1, :bar=>2}"
== OS Threads
== methods:
>> def foo(bar = 1, baz) p [bar, baz] end
>> foo("wtf")
[1, "wtf"]
>> def foo(a, *b, c) p [a,b,c] end
=> nil
>> foo(1, 2,3,4, 5)
[1, [2, 3, 4], 5]
== Oniguruma
Named captures:
>> md = "hello ruby world".match(/^hello (?<what>.+) world$/)
=> #<MatchData "hello ruby world" what:"ruby">
>> md.captures
=> ["ruby"]
>> md.names
=> ["what"]
>> md[:what]
=> "ruby"
== local/blocklocal variables:
>> i = :foo
>> [1,2,3].each{|i| p i}
1
2
3
=> [1, 2, 3]
>> i
=> :foo
== Enumerable
>> enum = %w[a b c d].map
=> #<Enumerator:0x432b14>
>> enum.with_index{|letter, index| puts "#{letter} is #{index}" }
a is 0
b is 1
c is 2
d is 3
>> [1,2,3,4].count{|i| i > 3 }
=> 1
>> arr.take(2)
=> [1, 2]
>> arr
=> [1, 2, 3, 4, 5]
>> arr.group_by{|i| i % 2 }
=> {1=>[1, 3, 5], 0=>[2, 4]}
>> enum = [1,2,3,4].to_enum
=> #<Enumerator:0x406140>
>> loop{ n = enum.next; p n }
1
2
3
4
>> [].to_enum.next
raises: StopIteration: iteration reached at end
>> loop{ raise StopIteration }
=> nil
== BasicObject
>> BasicObject.ancestors
=> [BasicObject]
>> Object.ancestors
=> [Object, PP::ObjectMixin, Kernel, BasicObject]
== m17n
* encodings
* string encodings (converting, multibyte #size etc)
* file encodings (åpne med angitt encoding) (File.open("foo.txt", "r:utf-8"))
>> "åke".size
=> 3
>> "abc".bytes
=> #<Enumerator:0x1eddf4>
>> "abc".bytes.each {|byte| p byte }
97
98
99
* "Magic comment":
# encoding: utf-8
>> π = Math::PI
>> p π
=> 3.14159265358979
== Rails
it works!
=== Marius føyer til…
==== Fibers (in progress)
f = Fiber.new do
a,b = 0,1
Fiber.yield a
Fiber.yield b
loop do
a,b = b,a+b
Fiber.yield b
end
end
10.times {puts f.resume}
==== When not valid in when statements:
case "me"
when String:
# raises
end
==== Method#to_proc
%w(john jane).collect(&:capitalize)
==== Nested methods
def visited
def visited
"been there, done that"
end
"first timer"
end
==== $"
Like $:
==== Integer niceties
1.odd?
2.even?
==== Method niceties
class A
def foo
end
end
a = A.new
a.method(:foo).receiver #=> The instance
a.method(:foo).owner #=> The class
Som betyr at du for eksempel kan finne ut om en metode kommer fra klasse eller mix-in
module Fooable
def foo
end
end
class Foo
include Fooable
end
f = Foo.new
f.method(:foo).owner #-> Fooable
==== Object#type gone forever
RUBY_VERSION #=> 1.9.1
"Ruby".type #=> NoMethodError: undefined method `type' for "Ruby":String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment