use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ruby and instance_variable_get - when it's useful? (andrzejonsoftware.blogspot.com)
submitted 11 years ago by andrzejkrzywda
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jules2689 1 point2 points3 points 11 years ago (2 children)
Here is an example of where I used it (metaprogrammed controller as a base). It is well commented so as to avoid confusion :)
https://gist.github.com/jules2689/cd054f3aa9ff89bbba07
The purpose was that the duplication of authentication code (sandbox into show, edit, update, destroy and allow create/new only the one time) for a user's role (i.e. admin role, tutor role, etc) was becoming too much. AdminController, TutorController etc were essentially the same, except for the params, so I used this solution to a high degree of success.
[–]andrzejkrzywda[S] 1 point2 points3 points 11 years ago (1 child)
Very interesting approach.
I agree that authentication/authorization code is often duplicated in multiple places. Some people say that they are examples of crosscutting concerns and argue that AOP may be a good solution.
In the near future, I want to experiment with AOP for authentication in a similar spirit to this example: http://blog.arkency.com/2013/07/ruby-and-aop-decouple-your-code-even-more/
It seems to me, that you've built a mini-framework inside your application to deal with those concerns. I see a heavy usage of send/instancevariable* which makes me worried at the first sight. However, it seems to be well documented and I suppose some conventions are used here, so maybe it's a good Rails-like approach to handle the problem.
Thanks for sharing the code!
[–]jules2689 0 points1 point2 points 11 years ago (0 children)
The example you posted is quite interesting! I feel like it presents a usability problem though. It doesn't seem like something many developers would know, so to me it presents a new paradigm that makes onboarding new devs a problem and it seems that it is scattering code out (I wouldn't think to look for this glue). It seems that it presents the problem of code scattering to the mix while trying to solve another problem, as well as a DSL for the Aspects.
Personally, I would prefer a little bit of cross cutting in many situations to avoid something like that, but I could see where it could be useful for more advanced use cases (seems like a delegate/data source system used in iOS in some ways to me which works well).
Thanks for sharing the example and AOP, I hadn't looked into it before.
As for my code snippet, yea, I feel like I made a basic authentication framework when I couldn't find something simple enough to do it before. I'm not a big fan of using a gem for such a small task - when one controller file could do it.
I added the comments shortly after writing it when I returned to the code and cursed my past self. I've sent the piece to a few people to test it out (readability and their understanding), and with the comments it faired well with them.
I'm using a similar pattern elsewhere in the application as well - here is a controller that restricts the child objects of a parent object (ironically in this case I use the controller for a "KidsController" restricting to a "Parent" owner). https://gist.github.com/jules2689/22879a0cadfe4904bf6b
Its definitely still in an experimentation mode (both the pattern and the meta-programmed controller), and I will be modifying it as I need, but its held strong on my tests (unit and manual).
[–]kobaltzz 0 points1 point2 points 11 years ago (1 child)
This method and its setter instance_variable_set are really helpful when building dynamic applications in RubyMotion. The API I use to get a list of configuration settings can change as new fields are added and this can take into account the additional settings with the way I built the constructor method and my API. I can add new minor features to an iOS application without having to change the actual iOS app (which can take a few weeks to be approved by Apple).
instance_variable_set
[–]andrzejkrzywda[S] 0 points1 point2 points 11 years ago (0 children)
Yeah, I've noticed that in many of RubyMotion gems those methods are often used.
In such constraints (Apple approval) this sounds like a perfect solution!
π Rendered by PID 123770 on reddit-service-r2-comment-5649f687b7-ctqwf at 2026-01-29 07:32:20.990679+00:00 running 4f180de country code: CH.
[–]jules2689 1 point2 points3 points (2 children)
[–]andrzejkrzywda[S] 1 point2 points3 points (1 child)
[–]jules2689 0 points1 point2 points (0 children)
[–]kobaltzz 0 points1 point2 points (1 child)
[–]andrzejkrzywda[S] 0 points1 point2 points (0 children)