Declaring variables in Ruby

Hi there. I just started learning Ruby and here is the code I'm stuck
at:

class Greeter
   def initialize(name = "World")
     @name = name
   end
   def say_hi
     puts "Hi #{@name}!"
   end
   end

Okay, in this program I declared variable name named "name" using @
symbol, so it's "@name" declared. My question is do I need to use
"initialize" everytime I create a variable?

···

--
Posted via http://www.ruby-forum.com/.

Excerpts from Rocky D.'s message of 2013-09-08 08:26:53 +0200:

Hi there. I just started learning Ruby and here is the code I'm stuck
at:

class Greeter
   def initialize(name = "World")
     @name = name
   end
   def say_hi
     puts "Hi #{@name}!"
   end
   end

Okay, in this program I declared variable name named "name" using @
symbol, so it's "@name" declared. My question is do I need to use
"initialize" everytime I create a variable?

I don't exactly understand what you mean. The initialize method is
automatically called whenever you create an instance of a class using
its .new method:

gr = Greeter.new #this calls initialize

Variables starting with @ are instance variables, that is variables tied
to a specific object. Usually, you create instance variables in
initialize so that they're availlable immediately after the object has
been created. This is not a requirement, however. For instance, if you
want to remember that your greeter has already greeted someone, you
could do this in your say_hi method:

def say_hi
  puts "Hi #{name}"
  @greeted = true
end

You create an instance variable in say_hi without having created it
in initialize. Personally, I don't like this much because I usually
prefer to have all instance variables visible at a glance, which is
easier if you create them all in the same place, with initialize being
the obvious choice.

Note that in ruby you don't "declare" variables: you just create them by
assigning a value to them. You can also use an instance or class variable you
haven't created yet: its value will be nil. In this case, however,
ruby will complain by issuing a warning (if you have warnings enabled).

class X

  def test_var
    p @var
  end

end

X.new.test_var
#=> nil
#If warnings are enabled, you'll get a warning saying
"warning: instance variable @var not initialized"

I hope this helps

Stefano

No, you can set an instance variable from anywhere. There are really only
two reasonable ways to set them, though. The first is in the initialize as
you've done, the second is in a getter method which will initialize the
variable the first time it is called. Using your example, that becomes:

class Greeter
  def say_hi
    puts "Hi #{@ame}!"
  end

  def name
    @name ||= 'World'
  end
end

-Josh

···

On Sun, Sep 8, 2013 at 1:26 AM, Rocky D. <lists@ruby-forum.com> wrote:

Hi there. I just started learning Ruby and here is the code I'm stuck
at:

class Greeter
   def initialize(name = "World")
     @name = name
   end
   def say_hi
     puts "Hi #{@name}!"
   end
   end

Okay, in this program I declared variable name named "name" using @
symbol, so it's "@name" declared. My question is do I need to use
"initialize" everytime I create a variable?

--
Posted via http://www.ruby-forum.com/\.

do I need to use "initialize" everytime I create a variable?

You can create an instance variable just about anywhere, in a method.

initialize() just happens to be (almost) the first method that is called
when one runs .new on a class (I think .allocate is the first one)

What I often do is this:

def initialize
  reset
end
def reset
  @happy_var = nil
end

To both satisfy the ruby parser, so that it knows it was
initialized, and to be able to do .reset on my object
to be able to reset it to its default, "native" state,
the way I need it.

···

--
Posted via http://www.ruby-forum.com/\.

Stefano Crocco wrote in post #1120935:

Excerpts from Rocky D.'s message of 2013-09-08 08:26:53 +0200:
Variables starting with @ are instance variables, that is variables tied
to a specific object. Usually, you create instance variables in
initialize so that they're availlable immediately after the object has
been created.
Note that in ruby you don't "declare" variables: you just create them by
assigning a value to them. You can also use an instance or class
variable you
haven't created yet: its value will be nil. In this case, however,
ruby will complain by issuing a warning (if you have warnings enabled).

class X

  def test_var
    p @var
  end

end

X.new.test_var
#=> nil
#If warnings are enabled, you'll get a warning saying
"warning: instance variable @var not initialized"

I hope this helps

Stefano

Thank you so much, some of my doubts are almost cleared :slight_smile:
I'm doing some practice, that is the only way I can get familiar with
this stuff.

···

--
Posted via http://www.ruby-forum.com/\.

unsubscribe

发自我的 iPhone

在 2013-9-16,1:21,Josh Cheek <josh.cheek@gmail.com> 写道:

···

On Sun, Sep 8, 2013 at 1:26 AM, Rocky D. <lists@ruby-forum.com> wrote:

Hi there. I just started learning Ruby and here is the code I'm stuck
at:

class Greeter
   def initialize(name = "World")
     @name = name
   end
   def say_hi
     puts "Hi #{@name}!"
   end
   end

Okay, in this program I declared variable name named "name" using @
symbol, so it's "@name" declared. My question is do I need to use
"initialize" everytime I create a variable?

--
Posted via http://www.ruby-forum.com/\.

No, you can set an instance variable from anywhere. There are really only two reasonable ways to set them, though. The first is in the initialize as you've done, the second is in a getter method which will initialize the variable the first time it is called. Using your example, that becomes:

class Greeter
  def say_hi
    puts "Hi #{@ame}!"
  end
  
  def name
    @name ||= 'World'
  end
end

-Josh