1

Defining an Uninitialized Instance Variable

 3 years ago
source link: https://www.codesd.com/item/defining-an-uninitialized-instance-variable.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Defining an Uninitialized Instance Variable

advertisements

Is it elegant to use instance variables in a class which are not initialized and setting them using other methods? Or maybe there is a better way to do that?

class Klass
  def initialize(a)
    @a = a
  end

  def set_b(b)
    @b = b
  end
end


In contrast to other languages, If you do not initialize an instance variable it will always be nil (whereas in certain other languages you could get something undefined).

As long as other methods of Klass do not depend on the instance variable actually having a value, this should be ok.

As for getters and setters, there are attr_accessor, attr_reader and attr_writer (see the docs).

class Klass
  attr_accessor :b
  # there's also attr_reader and attr_writer

  def initialize(a)
    @a = a
  end
end

k = Klass.new :foo
k.b = :bar

k.b
#=> :bar

k.a
#=> undefined method `a' for #<Klass:0x007f842a17c0e0 @a=:foo, @b=:bar> (NoMethodError)

Related Articles

Does the naming of an instance variable with the underline title as a prefix have side effects in Cocoa (Objective-C)?

Possible Duplicate: How does an underscore in front of a variable in a cocoa objective-c class work? I found that in Apple's frameworks' header files , Apple name instance variable with prefix underscope inside a class interface. like the _delegate i

Why can not I directly access the instance variables in the Active Record callback?

In Active Record callbacks, I always see examples using self.variable. Sometimes, the self. prefix isn't needed, so they use just variable (see this related question). From what I've read, these two methods of referencing the class instance variable

Ruby instance variables or local variables?

I am new to Ruby language. I understand that @@count: Class variables @name: Instance variables my_string: Local variables I keep the above in mind. However, I found one Ruby code like this: class HBaseController < ApplicationController ... def resul

memoization in rails using an instance variable

I'm following railstutorial by Michael Hartl. In chapter 8.2.2 he defines a variable @current_user and a method current_user. app/helpers/sessions_helper.rb looks like this: module SessionsHelper # Logs in the given user. def log_in(user) session[:us

Why does the local variable MUST be initialized and why instance variables MUST NOT be initialized before being used?

I've noticed that the JAVA allows to use uninitialized instance variable but it blocks to use uninitialized local variables. I want to know why the language is saying so? Note : This is not the first post of this kind. I've visited below questions to

ARC Memory Management: @Property (nonatomic, strong) VS instance variable

What I've found using the profiling tool in Xcode (Allocations) is that when you nil out a property it does not get deallocated until the parent class gets nilled out. Now let's say you want to make sure that you don't keep an expensive modal view co

Syntax for C # variables - why do you use the class name twice when defining a new instance?

Pretty simple really. Beginner to C# and struggling to understand the reasoning behind the syntax for assigning new instances to variables. Probably one of those things that you don't necessarily need to understand but would provide insight into how

Is it possible to call defined? on a dynamic instance variable name?

Is there a way to call check defined? on a dynamic instance variable name? What I want is to be able to do something like this: dynamic_attr = 'foo' define_method :my_method do if defined? instance_variable_get("@#{dynamic_attr}") # Obviously fa

Defining Objective-C private instance variables

Is there any difference in where we define private instance variables? As I understand there are two possibilties: 1) In header file @interface MyViewController : UIViewController { @private NSString *fooString; } 2) Second way is to define it in the

If we define our own constructor, how does java initialize the instance variables to their default value?

Java assigns default values to instance variables using default constructor. But if we define our own constructor then how does java give default values (because when we write our constructor then, then default constructor is not added).Java assigns

Define instance variables in Objective-C categories with associative references - Error & ldquo; Using the identifier not declared 'OBJC_ASSOCIATION_RETAIN & rdquo;

I am trying to create "fake" instance variables in categories by using objc_setAssociatedObjectas described in this post. However, I get the following error using ARC in iOS 6.1: Use of undeclared identifier 'OBJC_ASSOCIATION_RETAIN for the foll

Objective-C: Instance variable used while "self" is not defined and hellip; but he is

I've been tasked with cleaning up some Clang errors in a code base. I am very new to iPhone development and Objective C, but have found most of the problems trivial... this one is stumping me though, when I'm sure its something embarrassing. From a Z

The instance variable is not defined in Rails 4

I'm having a problem with a Rails 4 app that used to work perfectly fine. Now, an instance variable remains nil after being set. In the controller, I have the following code: def create @reportdate = Reportdate.new(reportdate_params) weekly_summary =

How to define a value of a text field if an instance variable is not null in the rails

I have a new product form, and in this form I'll either get to it by going to products/new, or by passing in an instance variable on a button press to pre-populate the form using data from a 'feature' instance variable. The code works for the most pa

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK