Skip to content

Instantly share code, notes, and snippets.

def add_listener(listener)
(@listeners ||= [] << listener)
end
add_listener("chris")
@listeners && @listeners.each do |x| # why not just @listeners.each ??
puts x
end
<!DOCTYPE html>
<html>
<head>
<style>
p { color:red;}
</style>
<body>
<p>
test
</p>

[4:06pm] butblack: ok, and why wouldn't the class just have those attributes in an initialize method?

[4:07pm] Spooner: Because it is marginally quicker to use Struct as a base, rather than explicitly have attr_accessors and a defined #initialize.
[4:07pm] Spooner: There isn't really much to gain from that idiom, unless Person is pretty trivial.
[4:09pm] butblack: Spooner: ok thanks
[4:10pm] Spooner: Better than that idiom, however, is: Person = Struct.new(:name, :age, :race) do; def hello; puts "Hello"; end; end
[4:10pm] Spooner: butblack, Since then you aren't creating a class just to be inherited from, but rather add other methods to a Struct.

module TestHelper
def person(name ="Sarah", &block)
Person.new(self, name, block)
end
end
class Person < Struct.new(:view, :age, :callback)
actionpack (3.2.1) lib/action_view/helpers/url_helper.rb:618:in `convert_options_to_data_attributes'
actionpack (3.2.1) lib/action_view/helpers/url_helper.rb:241:in `link_to'
actionpack (3.2.1) lib/action_view/helpers/url_helper.rb:235:in `link_to'
app/views/users/test.html.haml:1:in `_app_views_users_test_html_haml___711649185872844294_70280432259240'
actionpack (3.2.1) lib/action_view/template.rb:143:in `block in render'
activesupport (3.2.1) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.1) lib/action_view/template.rb:141:in `render'
actionpack (3.2.1) lib/action_view/renderer/template_renderer.rb:41:in `block (2 levels) in render_template'
actionpack (3.2.1) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `block in instrument'
error:
No error without the view portion and can do ... route?page=2
however when adding in the view it gets a no route error
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
PS1="\w Δ "
PATH=$PATH:~/bin
export PATH
2013-06-19T01:13:01.124408+00:00 app[web.1]: Started PUT "/articles/5-Literary-Compositions-" for 99.232.11.181 at 2013-06-19 01:13:01 +0000
2013-06-19T01:13:01.445438+00:00 heroku[router]: at=info method=POST path=/articles/5-Literary-Compositions- host=hidden-taiga-5779.herokuapp.com fwd="99.232.11.181" dyno=web.1 connect=2ms service=516ms status=302 bytes=138
2013-06-19T01:13:01.443470+00:00 app[web.1]: Processing by ArticlesController#update as HTML
2013-06-19T01:13:01.443470+00:00 app[web.1]: #post information was in here, large post
2013-06-19T01:13:01.443470+00:00 app[web.1]: Redirected to http://hidden-taiga-5779.herokuapp.com/articles/5-Literary-Compositions-
= link_to "Articles Home", user_article_path(@user.username.to_s, (Date.today).strftime("%Y"), (Date.today).strftime("%m"), (Date.today).strftime("%d"))
@1dolinski
1dolinski / user.rb
Last active December 19, 2015 04:28
[17] pry(main)> ids = User.all.map(&:id)
=> [2, 1, 3, 4, 5]
[19] pry(main)> ids.each do |id|
[19] pry(main)* @users = User.where(id: id) rescue User.where(id: id)
[19] pry(main)* end
=> [2, 1, 3, 4, 5]
Why doesn't this return all of the instances of User?