1

I have recently learned how to create classes, although I am not ENTIRELY sure where and why I should use them. I'd use them to create objects, which have similar methods/properties.

I tried making a gag code, but I stumbled upon a question I can't find an answer to.

class Person
  def initialize(name,health)
    @name = name
    @hp = health
  end

  def kick
    @hp -= 1
    if (@hp <= 0)
      puts "#{@name} got REKT!"
    end
  end

end

#Friends
michael = Person.new("Michael", 10)

10.times { michael.kick }

Even though this code works, I'm wondering if it is possible to use/call mihchael's hp outside the class? Perhaps sort of like a hash? michael[@hp]? But this doesn't work, even if i set hp to be a global variable. Should all if/else statements who check object's properties be inside the class?

Thank you very much

6
  • i guess you are looking for attr_accessor. attr_accessor :hp Commented May 6, 2016 at 18:29
  • 3
    I would recommend reading about attr_accessor over at this SO question: stackoverflow.com/questions/4370960/… Commented May 6, 2016 at 18:31
  • 5
    Please paste your code as text, not a picture, to make it easier to modify. Commented May 6, 2016 at 18:35
  • Do you really expect readers who wish to modify your code to copy it manually, line-by-line? Commented May 6, 2016 at 18:50
  • What is "gag code"? Is it related to shutting someone up, pranks or choking? Commented May 6, 2016 at 18:57

1 Answer 1

5

The standard way to do this in Ruby is to create an accessor:

class Person
  attr_reader :name
  attr_reader :hp
end

Then outside the class you can call things like:

puts "#{michael.name} has only #{michael.hp} HP left"

The reason you create objects is to organize your code and data into logical contexts and containers.

Sign up to request clarification or add additional context in comments.

13 Comments

...but if there are no accessors, all is not lost: puts "#{michael.instance_variable_get(:@name)} has only #{michael.instance_variable_get(:@hp)} HP left" #=> "Michael has only 10 HP left". See Object#instance_variable_get.
@CarySwoveland True, but that's the equivalent of reaching into someone's pants to grab their keys. It's not very polite. An accessor is the best way to ask for permission, plus it means the class is free to change how those values are stored.
@tadman if attr_reader :keys is used then we do not hold the \@keys attribute in the pants pocket, but advertise it as 'the \@keys are on the hook next to the door'. You do not understand how attr_reader :keys works. Basically it tells interpreter do define def keys; \@keys ;end . So when you remove attr_reader and define method keys with you can easily have your changes done.
@ruby_object Now I'm even more confused what your objection here is. Yes, it's a class method and that's why I'm using it in the class context. This is textbook how it's done. If there was a mistake in a book I don't see how that's relevant.
@ruby_object If there's any misunderstanding here I think it's your understanding of the question. The question was "how do I access instance variables" and the most idiomatic answer is "use attr_reader". Yes, attr_reader is a class method, and obviously so since it's used to alter the properties of the class and introduce these new instance methods dynamically.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.