Forum: Ruby Declaring variables in Ruby

75eb775a3636c4dbc0c305ae3682df31?d=identicon&s=25 Rocky D. (rocky_d)
on 2013-09-08 08:26
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?
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2013-09-08 08:47
(Received via mailing list)
Excerpts from Rocky D.'s message of 2013-09-08 08:26:53 +0200:
>      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
75eb775a3636c4dbc0c305ae3682df31?d=identicon&s=25 Rocky D. (rocky_d)
on 2013-09-08 09:05
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 :)
I'm doing some practice, that is the only way I can get familiar with
this stuff.
B31e7abd14f1ceb4c4957da08933c630?d=identicon&s=25 Josh Cheek (josh-cheek)
on 2013-09-15 19:22
(Received via mailing list)
On Sun, Sep 8, 2013 at 1:26 AM, Rocky D. <lists@ruby-forum.com> wrote:

>      puts "Hi #{@name}!"
> 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
E5e6392782fb2b1856409b89a62ae9f8?d=identicon&s=25 xiangzhuyuan (Guest)
on 2013-09-16 06:57
(Received via mailing list)
unsubscribe

ҵ iPhone

 2013-9-161:21Josh Cheek <josh.cheek@gmail.com> д
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2013-09-16 16:19
> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.