How do I give a instance variable a default value

Hello,

I have this :

class Account

  attr_reader :name

  attr_reader :balance

 

  def initialize (name, balance)

      @name = name

      @balance = balance

      balance = 100

  end

end

But when I run it I see this error message : **Oops, try again.**
Did you give your initialize method's balance parameter a default

value?

Can anyone explain to me what I did wrong ?

Roelof

I cannot reproduce:

2.0.0p195 :001 > class Account
2.0.0p195 :002?> attr_reader :name
2.0.0p195 :003?> attr_reader :balance
2.0.0p195 :004?>
2.0.0p195 :005 > def initialize (name, balance)
2.0.0p195 :006?> @name = name
2.0.0p195 :007?> @balance = balance
2.0.0p195 :008?> balance = 100
2.0.0p195 :009?> end
2.0.0p195 :010?>
2.0.0p195 :011 > end

2.0.0p195 :013 > Account.new "test", 150
=> #<Account:0x000000013e8ab8 @name="test", @balance=150>
2.0.0p195 :014 > Account.new("test", 150).balance
=> 150

In what kind of environment are you testing this? The "Ooops, try
again" message seems very weird to me...

Jesus.

···

On Mon, May 26, 2014 at 5:46 PM, Roelof Wobben <r.wobben@home.nl> wrote:

Hello,

I have this :

class Account
  attr_reader :name
  attr_reader :balance

  def initialize (name, balance)
      @name = name
      @balance = balance
      balance = 100
  end

end

But when I run it I see this error message : Oops, try again. Did you give
your initialize method's balance parameter a default value?

Can anyone explain to me what I did wrong ?

You are setting value of balance attribute (instead of instance variable)
to 100, but it's never used, so you can delete that line.

To make an attribute optional and give it a default value, you must provide
that value in method definition:

def initialize(name, balance=100)
  ...
  ...

I hope this helps :slight_smile:

···

On 26 May 2014 17:46, Roelof Wobben <r.wobben@home.nl> wrote:

Hello,

I have this :

class Account
  attr_reader :name
  attr_reader :balance

  def initialize (name, balance)
      @name = name
      @balance = balance
      balance = 100
  end

end

But when I run it I see this error message : *Oops, try again.* Did you
give your initialize method's balance parameter a default value?

Can anyone explain to me what I did wrong ?

Roelof

@Jesus it seems to me he is using an online tutorial/trainer that prompts
for a certain change and responds based on user action

@Roelof take a look at
http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls#Default_Values

···

On Mon, May 26, 2014 at 11:58 AM, Jesús Gabriel y Galán < jgabrielygalan@gmail.com> wrote:

On Mon, May 26, 2014 at 5:46 PM, Roelof Wobben <r.wobben@home.nl> wrote:
> Hello,
>
> I have this :
>
> class Account
> attr_reader :name
> attr_reader :balance
>
> def initialize (name, balance)
> @name = name
> @balance = balance
> balance = 100
> end
>
> end
>
> But when I run it I see this error message : Oops, try again. Did you
give
> your initialize method's balance parameter a default value?
>
> Can anyone explain to me what I did wrong ?

I cannot reproduce:

2.0.0p195 :001 > class Account
2.0.0p195 :002?> attr_reader :name
2.0.0p195 :003?> attr_reader :balance
2.0.0p195 :004?>
2.0.0p195 :005 > def initialize (name, balance)
2.0.0p195 :006?> @name = name
2.0.0p195 :007?> @balance = balance
2.0.0p195 :008?> balance = 100
2.0.0p195 :009?> end
2.0.0p195 :010?>
2.0.0p195 :011 > end

2.0.0p195 :013 > Account.new "test", 150
=> #<Account:0x000000013e8ab8 @name="test", @balance=150>
2.0.0p195 :014 > Account.new("test", 150).balance
=> 150

In what kind of environment are you testing this? The "Ooops, try
again" message seems very weird to me...

Jesus.

I have never seen the "Oops" message either. Maybe this what you are
looking for:

def initialize (name, balance=100)

···

On Mon, May 26, 2014 at 11:58 AM, Jesús Gabriel y Galán < jgabrielygalan@gmail.com> wrote:

On Mon, May 26, 2014 at 5:46 PM, Roelof Wobben <r.wobben@home.nl> wrote:
> Hello,
>
> I have this :
>
> class Account
> attr_reader :name
> attr_reader :balance
>
> def initialize (name, balance)
> @name = name
> @balance = balance
> balance = 100
> end
>
> end
>
> But when I run it I see this error message : Oops, try again. Did you
give
> your initialize method's balance parameter a default value?
>
> Can anyone explain to me what I did wrong ?

I cannot reproduce:

2.0.0p195 :001 > class Account
2.0.0p195 :002?> attr_reader :name
2.0.0p195 :003?> attr_reader :balance
2.0.0p195 :004?>
2.0.0p195 :005 > def initialize (name, balance)
2.0.0p195 :006?> @name = name
2.0.0p195 :007?> @balance = balance
2.0.0p195 :008?> balance = 100
2.0.0p195 :009?> end
2.0.0p195 :010?>
2.0.0p195 :011 > end

2.0.0p195 :013 > Account.new "test", 150
=> #<Account:0x000000013e8ab8 @name="test", @balance=150>
2.0.0p195 :014 > Account.new("test", 150).balance
=> 150

In what kind of environment are you testing this? The "Ooops, try
again" message seems very weird to me...

Jesus.

Thanks , too clarify. Im using
CodeAcademy to learn Ruby.

  Another problem.

  You can do this

  x = y ? puts x : puts y

  but what if you want to do multple things like this

  x = y ? x -= 1; puts x : y-= 1 ; puts y

  I see then a error message that the ; is found where a : is

expected

  folding it into {} also do not seem to do the trick.

  Roelof

  Marian Mosley schreef op 26-5-2014 18:05:
···
    I have never seen the "Oops" message either.  Maybe

this what you are looking for:

    def initialize (name, balance=100)
      On Mon, May 26, 2014 at 11:58 AM, Jesús > Gabriel y Galán <jgabrielygalan@gmail.com> >           wrote:
            On Mon, May 26, 2014 at 5:46 PM, Roelof > > Wobben <r.wobben@home.nl                > > > wrote:

            > Hello,

            >

            > I have this :

            >

            > class Account

            >   attr_reader :name

            >   attr_reader :balance

            >

            >   def initialize (name, balance)

            >       @name = name

            >       @balance = balance

            >       balance = 100

            >   end

            >

            > end

            >

            > But when I run it I see this error message : Oops,

try again. Did you give

            > your initialize method's balance parameter a

default value?

            >

            > Can anyone explain to me what I did wrong ?

I cannot reproduce:

        2.0.0p195 :001 > class Account

        2.0.0p195 :002?>     attr_reader :name

        2.0.0p195 :003?>     attr_reader :balance

        2.0.0p195 :004?>

        2.0.0p195 :005 >       def initialize (name, balance)

        2.0.0p195 :006?>           @name = name

        2.0.0p195 :007?>           @balance = balance

        2.0.0p195 :008?>           balance = 100

        2.0.0p195 :009?>       end

        2.0.0p195 :010?>

        2.0.0p195 :011 >     end



        2.0.0p195 :013 > Account.new "test", 150

         => #<Account:0x000000013e8ab8 @name="test",

@balance=150>

        2.0.0p195 :014 > Account.new("test", 150).balance

         => 150





        In what kind of environment are you testing this? The

"Ooops, try

        again" message seems very weird to me...



        Jesus.

You need to use parenthesis:

x == y ? (x -= 1; puts x) : (y-= 1 ; puts y)

By the way, I think you are missing one equals sign in the condition,
otherwise it's an assignment, so it will be true unless y is nil.

Jesus.

···

On Mon, May 26, 2014 at 7:15 PM, Roelof Wobben <r.wobben@home.nl> wrote:

Thanks , too clarify. Im using CodeAcademy to learn Ruby.

Another problem.

You can do this

x = y ? puts x : puts y

but what if you want to do multple things like this

x = y ? x -= 1; puts x : y-= 1 ; puts y

I see then a error message that the ; is found where a : is expected
folding it into {} also do not seem to do the trick.