0

Here is my code

This code creates an instance of the Blub class, then prints two strings. For some reason @soaps_a doesn't work.

My code is:

class Blub


  # This creates 3 strings
  @soaps_a = "soap a"


  def say_hello()

    puts "hi"

    puts @soaps_a

  end


end

# Bunch of Blubs
w = Blub.new()
w.say_hello()

and the output is:

hi

but for some reason @soaps_a (which is 'soap a') doesn't work.

The output should be

hi
soap a

Why are strings not working in Ruby? @soap_a is a local variable, and should be accessible in say_hello, but it isn't printing so IDK? :/

any help would be appreciated, why isn't my string printer working?

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Adjam
  • 598
  • 1
  • 8
  • 22
  • 2
    "@soap_a is a local variable" - no, it's not. It's an instance variable and its instance is not what you think it is. Set it in your `def initialize` method. – Sergio Tulentsev Nov 01 '18 at 16:16
  • 1
    Probably not important, but *"# This creates 3 strings"* -- No it doesn't? If you've omitted the code that the comment refers to, then it's best to delete the comment too, otherwise it causes confusion! – Tom Lord Nov 01 '18 at 16:41
  • 1
    Note: It's a good habit to omit empty parentheses when making method calls in Ruby. They're not necessary and just add clutter: `Blub.new` instead of `Blub.new()` – tadman Nov 01 '18 at 17:59

0 Answers0