Skip to content

Instantly share code, notes, and snippets.

@jah2488
Last active November 22, 2016 19:47
Show Gist options
  • Select an option

  • Save jah2488/8f4e3b968289107e870b to your computer and use it in GitHub Desktop.

Select an option

Save jah2488/8f4e3b968289107e870b to your computer and use it in GitHub Desktop.
Ruby will allow you to do some pretty amazing... errr terrifying things.
class Foo
def initialize(default = case $glob
when "foo" then (def foobar; "hi!" end)
end)
@default = default
end
end
Foo.new #=> #<Foo:0x007f849f37be98 @default=nil>
$glob = 'foo'
Foo.new #=> #<Foo:0x007f849bbf47e0 @default=:foobar>
Foo.new.methods.first #=> :foobar
class Foo
attr_accessor :bar,
:baz,
def initialize(foo)
@foo = foo
end
end
Foo.new('my-bar') #=> ArgumentError: wrong number of arguments (1 for 0) 😕
Foo.new.methods.take(6) #=> [:foobar, :bar, :bar=, :baz, :baz=, :initialize=] 😰
f = Foo.new
f.initialize = 'hello'
f.initialize # => 'hello'
#Thanks to Gavin Stark for pointing this one out.
<section>
<%= render partial: 'posts/form', locals: {:@user => current_user, :@post => post} %>
</section>
<!-- This will re-assign the instance variables @user and @post -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment