elz: (sisko)
[personal profile] elz posting in [community profile] rubyonrails
I was trying to explain this to someone earlier while doing a couple other things and was making a muddle of it. So here's a somewhat-more-coherent explanation:



Ruby



If the terms 'class' and 'instance' are still unfamiliar to you, think of it this way:

class : instance
Superhero : batman
Vulcan : spock
Planet : earth
Website : dreamwidth

A class is a general type of thing, an instance is one of those things. So it follows pretty logically that a class variable stores information about the whole class and an instance variable stores information about an instance of that class. @@this is a class variable and @this is an instance variable.

Let's say my Superhero class looks like this:

class Superhero
  def secret_identity=(name)
    @secret_identity = name  
  end
	
  def secret_identity
    @secret_identity
  end
end


I can then create a new superhero like this:

batman = Superhero.new
batman.secret_identity = "Bruce Wayne"


And then when I type in batman.secret_identity again, it should say "Bruce Wayne". If I then do:

superman = Superhero.new
superman.secret_identity = "Clark Kent"


batman.secret_identity is still "Bruce Wayne", and superman.secret_identity is "Clark Kent".

So what you'll notice is:

  1. Each instance can have its own @secret_identity, and they don't conflict with one another.

  2. The value of @secret_identity sticks around and can be accessed and changed by any of the instance methods in the class. (You don't want to use instance variables in class methods, though. Badness will ensue.)

  3. I did have to create methods to let you access the instance variable from outside the class - you can't get to it directly.

  4. @secret_identity happens to be a string, but you could have an instance variable called @nemeses that stored an array of supervillains. Like all Ruby variables, instance variables can contain anything.


(NB: in practice, of course, always take steps to keep your secret identity from being read by the wrong people.)

Class variables are a bit less common, but you can imagine that the Vulcan class has something like this: @@shape_of_ears = "pointy". Doesn't really matter if you're talking about Spock or T'Pol: each Vulcan has pointy ears, and all Vulcans have pointy ears. The danger is that if you change @@shape_of_ears anywhere, it's changed everywhere. And you don't want to mess with Spock's ears.

Rails



So now we come to Rails, and if you've used Rails a bit already, you may be more rather than less confused, because instance variables are usually used in controllers and views, and they're specifically used to send information from the controller to the view. I put a bunch of people in @people in my controller action, and I can get those same people out of @people in my view file. What happened to not being able to access instance variables from outside the class they belong to?

This is actually Rails pulling a fast one on you. You're not accessing the controller's instance variables; what happens is that when a view is rendered, Rails takes all the instance variables in the controller action, clones them, and hands the clones off to the view. That's why you can get to the values of those variables when you render a view (either by default or by using 'render'), but not when you redirect - redirect_to sends you to a different controller action which renders its own view, giving you a whole different army of clones. So to speak.



Let me know if you have any questions, additions or corrections!

(no subject)

Date: 2009-04-17 08:49 am (UTC)
zooey_glass: Sunset skyline (Default)
From: [personal profile] zooey_glass
Elz, this is awesome! So clear and understandable. I am somewhat in love with your teaching skills <3

bGwEQvVLTfo

Date: 2013-05-28 05:31 am (UTC)
From: (Anonymous)
If I communicated I could thank you enguoh for this, I'd be lying.

upBVyYzdFSfkcZoGG

Date: 2013-05-26 08:24 am (UTC)
From: (Anonymous)
I think the core ethical value that is tuhgot in the book Seedfolks is respect. I think this because all of the neighbors, even though they do not know each other, they still respect the fact that they are people and should be treated as such. All the neighbors treat each other fairly and never desturb other peoples garden. This is why I think respect is the core ethical value that is tuhgot in the book Seedfolks .

(no subject)

Date: 2009-04-18 01:33 am (UTC)
stultiloquentia: Campbells condensed primordial soup (Default)
From: [personal profile] stultiloquentia
Can I just pop in to say OMG AWESOME? I got as far as installing Rails back when AO3 was starting up, but I got, er, derailed by RL for a while, and then it seemed like I was just too far behind to be of use to anybody. But I'd still like to learn. You're a really clear teacher.

afRdpumSfSACKyu

Date: 2013-05-28 05:34 am (UTC)
From: (Anonymous)
It's a real pleausre to find someone who can think like that

btGSkZlXgO

Date: 2013-06-11 12:01 pm (UTC)
From: (Anonymous)
I am of the opinion that one of the ioatrmpnt issues driving the one percenters that has gone unmentioned in the health care debate, is that permitting their employees and the rest of the public access to affordable medical care means that that we would be making our own choices, our own decisions. Instead of being commanded from the top down as to what what we are to be permitted. This displays the utter contempt our self-designated overlords have for all us human cattle.

GpDqDdNXpToy

Date: 2013-05-26 06:22 am (UTC)
From: (Anonymous)
Beard of Odin, this is SO MUCH AWESOME! I usually get to see what the ctiornbutors are up to, but it's rare I get a look at what other commenters do more posts, please! Nothing proper on the drawing table at the mo, but here's some sketches done up this week after reading an article on the latest fossil evidence for dinosaur colours (iridescence in Microraptor feathers, this time). I really don't know a damn thing about dino anatomy, but hopefully 'bird with lizard head' isn't too far off.

(no subject)

Date: 2009-04-18 05:58 pm (UTC)
From: [personal profile] ex_rustler489
Oh, bless you. As someone coming in with zero programming experience, the terminology is one of the biggest stumbling blocks. This is marvelous. Thanks!

cxrAopYAhUGQC

Date: 2013-05-28 07:02 am (UTC)
From: (Anonymous)
The core ethical value that shows most in the book Seedfolks is recsept because when they come together to grow the garden, they have to recsept everyones ideas, personalities, cultures, races, and abilities. Without recsept they wouldn't be able to work together growing the garden, and nobody would like each other.

ISvinBHuCWroLifD

Date: 2013-06-08 07:16 pm (UTC)
From: (Anonymous)
I just created a brand new SC 6.2 site and added the CustomItemGenerator scuore to my solution as another project. I installed the package into my site, then built the scuore to overwrite the package's CustomItemGenerator.dll. I tried to generate custom item classes for a sample dummy template and the generate button doesn't seem to do anything still.I checked what the button does in the core and saw it calls devtools:generatecustomitem(id=$Target) so I'm thinking maybe something isn't hooking in. I looked in the CustomItemGeneratorCommand class and saw that if you try to generate custom items on any items other than a template or template folder, there should be a sheer alert in SC, which I don't get.

goHcjAveZuEfCa

Date: 2013-06-09 01:38 am (UTC)
From: (Anonymous)
Thanks for keeping on top of this whole mess, and for surmamizing it in such a clear way. ("senior wtf correspondent" is such an appropriate tag for it) I guess I should start a Dreamwidth account as a back up or something, but most of my f-list and communities are staying, and I'm not active in any fandoms to expend the energy of finding and participating in new communities and all that. And maybe I'm just being too cynical here, but if Dreamwidth keeps getting more popular and expanding, then sooner or later the costs of operation will force them to choose whether or not to they view their userbase as a product to be sold. Same as LJ and all the other social networks.One thing is for sure: this whole debacle has made me so glad that I never signed up for Facebook. This certainly puts in me in the minority in my age group and has probably cost me, socially speaking, but man I sure as hell don't plan on signing up any time soon.

(no subject)

Date: 2009-05-25 10:12 pm (UTC)
tassosss: Shen Wei Zhao Yunlan Era (Default)
From: [personal profile] tassosss
I like this explanation, but I have a potentially stupid question: is "instance" another way of saying "object"?

hkRHlRPjfxcvLVkR

Date: 2013-05-26 07:14 pm (UTC)
From: (Anonymous)
public static boeolan contains(String left, String right) {int magicLeft = toMagicInt(left);int magicRight = toMagicInt(right);return (magicLeft & magicRight) == magicRight;}public static int toMagicInt(String text) {int result = 0;int index = 0;for (char c : text.toLowerCase().toCharArray()) {index = c a';result |= 2 << index;}return result;}

JkffYZKlPicdpps

Date: 2013-05-28 06:07 am (UTC)
From: (Anonymous)
Hi Joel. I've noticed you guys rencltey through Google alerts Yeah, there's much to do here. I've set the project aside for now. Might get back to it later.

MgfQHCSDMYSFwfeDjKm

Date: 2013-05-28 04:56 am (UTC)
From: (Anonymous)
Yes, these types of communities are ecxltay what we need. I am not a Ruby developer, but rather an InfoSec practitioner. I find myself often thinking about how to build such welcoming communities that bridge the gap between newcomer and guru. I will definitely be watching this space and see how you fellows do.

KVwdsCZyDPJUebpH

Date: 2013-05-28 06:56 am (UTC)
From: (Anonymous)
The core ethical value dettrsmnaoed in Seedfolks is responsibility because all the characters are responsible for taking care of their garden. Another core ethical value shown is respect because since all the characters are meeting one another throughout the process of creating this garden, they are also getting to know each other, and it takes respect to do that.

mXMqEcNzKtdRSFRqH

Date: 2013-06-09 12:16 am (UTC)
From: (Anonymous)
The end goal for BrowserID involves naitve browser support for authentication, using public-key crypto with the keys generated and stored by the browser. browserid.org exists as a compatibility shim to make BrowserID universally available *before* browsers have naitve support for it; they really need to document that plan better on the site.(Also, you could implement BrowserID server-side without JavaScript; browserid.org implements it client-side using JavaScript to avoid the need for specialized server-side code on each site adopting it, which greatly simplifies adoption.)OpenID requires a webpage supporting a specialized authentication protocol which websites don't normally implement. You can't use OpenID with an arbitrary homepage URL without either hosting an OpenID implementation yourself or delegating to a third-party authentication provider. More importantly, having a personal OpenID that doesn't depend on a third-party account requires hosting a personal homepage, which not every Internet user has or needs. As a result, in practice, the vast majority of OpenID users rely on a third-party identity provider such as the dozen whose icons you show at the top of this form, and that seems unlikely to ever change.BrowserID specifically ties identity to an email address, which everyone with Internet access already has. People can easily obtain an email address in their own name and at their own domain, and use that as their BrowserID, *without* having to seek out additional support for a specialized protocol as in OpenID. Also, almost all sites already want an email address when signing up, so using BrowserID avoids the need to deal with other pieces of information. BrowserID supports changing that email address, so that you don't get stuck with a pile of accounts using when you've migrated to . BrowserID also pre-validates the email address, which means you don't have to redo the usual email-a-link verification with every single site.You mentioned that you have three email addresses and only one homepage. That case actually works *better* with BrowserID, which offers you the choice of which email address you want to use to authenticate to any given site. So, you can easily maintain accounts for , , or , without needing to host three homepages or rely on third-party authentication providers.As for using multiple browsers, BrowserID supports migrating from browserid.org to in-browser authentication without changing anything visible to sites, and browsers which support in-browser authentication will almost certainly support migrating or sharing identities between themselves.So, I personally prefer BrowserID because it gives me more control over my own identity, lets me maintain multiple identities as easily as having multiple email addresses, avoids tying my account with a site to an account I currently have with a third party (and may want to drop in the future), reduces the number of pieces of information I need to provide to sites to one, eliminates the per-site email verification, and provides a path leading to in-browser crypto-based authentication.Does that help answer your question?

QOwpSgNFzHORGZcuq

Date: 2013-06-09 09:08 am (UTC)
From: (Anonymous)
Hi Gabriel,I have incorporated CustomItemGenerator' into my prjecots and now I want to work with automated unit testing using NUnit Framework on one of my project.When I access the properties (like CustomTextField) generated through CustomItemGenerator' in NUnit GUI it throws error, below is the stack trace of error -at Sitecore.Pipelines.RenderField.RenderFieldArgs..ctor()at Sitecore.Web.UI.WebControls.FieldRenderer.RenderField()at Sitecore.Web.UI.WebControls.FieldRenderer.Render()at CustomItemGenerator.Fields.BaseCustomField`1.get_Rendered()at CustomItemGenerator.Fields.SimpleTypes.CustomTextField.get_Text()But when I create my own properties (string type) in the same class (generated through CustomItemGenerator'), they are accessible in NUnit GUI.It would be great if you can provide some help on this problem.

Hello my friends!

Date: 2009-09-17 07:32 pm (UTC)
From: (Anonymous)
Hello my friends!

dveeQvPsQxKZyVWiHbu

Date: 2013-05-28 05:20 am (UTC)
From: (Anonymous)
I'm having prmloebs doing exactly this My mainpage.php file goes like this:And anotherpage.php goes like this:This is the string: When I view mainpage.php, I simply get:This is the string: (The variable is not being passed)Help!

July 2010

S M T W T F S
    123
4567 8910
11121314151617
18192021222324
25262728293031

Style Credit

Expand Cut Tags

No cut tags