Forum: Ruby class method

614aa6cb0cc315b8ef993dea32679642?d=identicon&s=25 John Yd (abettertomorrow80)
on 2014-08-06 00:55
class Boogeyman

@@allMonsterLocations = []
@@allMonsterNames = []
@@allMonsterAges = []
@@allMonsterWeights = []
@@allMonsterHeights = []
def initialize(name, location, age, weight, height)
@name = name
@location = location
@age = age
@weight = weight
@height = height

@@latestMonster = name
@@latestMonsterLocation = location

@@allMonsterNames << name
@@allMonsterLocations << location
@@allMonsterAges << age
@@allMonsterWeights << weight
@@allMonsterHeights << height
end

def self.latest_monster

puts "The latest monster is #{@@latestMonster}."
puts "He is in #{@@latestMonsterLocation}."

end

def self.MonsterAges
  p @@allMonsterAges
end

def self.OldestMonster
  i=0
  oldestMonster = @@allMonsterAges[0]
  while i < @@allMonsterAges.size - 1
    if oldestMonster < @@allMonsterAges[i]
      oldestMonster = @@allMonsterAges[i]
    end
    i+=1
  end
  p oldestMonster
end

def self.NameofOldestMonster(*allMonster)
  i=1
  oldestMonster = allMonster[0]
  while i < allMonster.size
    if oldestMonster.age < allMonster[i].age
      oldestMonster = allMonster[i]
    end
    i+=1
  end
  p oldestMonster
end


def self.MonsterLocations
  p @@allMonsterLocations
end

def self.MonsterNames
  p @@allMonsterNames
end

def self.MonsterHeights
  p @@allMonsterHeights
end

def self.MonsterWeights
  p @@allMonsterWeights
end

end
###################################################

monster1 = Boogeyman.new("Macabre Mac", "Seattle, WA", 52, 10, 300)

monster2 = Boogeyman.new("Gory Gary", "Reston, WV", 12, 5, 100)

monster3 = Boogeyman.new("King Cobra", "Hong Kong, China", 22, 15, 650)

puts
puts "Boogeyman.latest_monster is below"
Boogeyman.latest_monster

puts
puts "Boogeyman.MonsterLocations is below"
Boogeyman.MonsterLocations

puts

puts "Boogeyman.MonsterNames is below"
Boogeyman.MonsterNames

puts
puts "Boogeyman.MonsterAges is below"
Boogeyman.MonsterAges

puts
puts "Boogeyman.MonsterHeights is below"
Boogeyman.MonsterHeights

puts
puts "Boogeyman.MonsterWeights is below"
Boogeyman.MonsterWeights

puts
puts "Boogeyman.OldestMonster is below"
Boogeyman.OldestMonster


*array = [monster1, monster2, monster3]
puts
puts "Name of the oldest monster is below"
Boogeyman.NameofOldestMonster(*array)


###################################
I need help implementing the method Boogeyman.NameofOldestMonster

This is the new thing I just encounter and have no idea how to overcome
it. Please help. THANKS!
4a65f01f7ece0b720bdb0de3c3db089e?d=identicon&s=25 Dansei Yuuki (blutorange)
on 2014-08-06 19:54
Here are some things I would like to point out:

>    def foobar(*args)

A starred argument means that ruby should take all arguments that may be
given (from this position onwards), and store them in an array that can
be accessed by the variable monsters.

>    def foobar(*args)
>        p args.class
>        p args.first
>    end
>    foobar(1,2,3,4)
>    foobar(*[1,2,3,4])
>    foobar([1,2,3,4])

The above would print
    Array
    1
    Array
    1
    Array
    [1,2,3,4]

So in your case, you could simply call

>    Boogeyman.NameofOldestMonster(monster1, monster2, monster3)

When your write

>    oldestMonster.age

your are calling the method 'age' for oldestMonster, which is an
instance of Boogeyman. But you did not define any instance method called
'age' for your class. In order to access instance variables, you have to
add a method like this:

>    class Boogeyman
>        def age
>            @age
>        end
>    end

Doing this for every instance variable can get tedious, so ruby provides
a shortcut for this:

>    class Boogeyman
>        attr_reader :age
>    end

attr_reader (=attribute reader) is a class method which, when called,
creates a new instance method with the same effect as ours above -
returning the instance variable @age.


I'm not certain what exactly you are trying to do, but here's a working
example for your that I think is much easier to read:

>    class Monster
>        attr_reader :age
>        attr_reader :name
>        def initialize(name, age)
>            @age = age
>            @name = name
>        end
>        def inspect
>            "Monster<#{@name},#{@age}>"
>        end
>    end
>
>    class MonsterGroup
>        def initialize
>            @monsters = []
>        end
>        def push(monster)
>            @monsters.push(monster)
>        end
>        def names
>            @monsters.map { |m| m.name }
>        end
>        def last
>            @monsters.last
>        end
>        def oldest
>            @monsters.max { |a,b| a.age <=> b.age }
>        end
>    end
>
>    my_group = MonsterGroup.new
>    my_group.push Monster.new('Max', 25)
>    my_group.push Monster.new('Moritz', 55)
>    my_group.push Monster.new('James', 12)
>
>    p my_group.names       # => ["Max","Moritz","James"]
>    p my_group.last.name   # => "James"
>    p my_group.last.age    # => 12
>    p my_group.oldest      # => Monster<Moritz,55>
>    p my_group.oldest.name # => "Moritz"
>    p my_group.oldest.age  # => 55

The above uses several methods from ruby's core library, such as
Enumerable.max and Array#last. They are all documented here
http://www.ruby-doc.org/core-2.1.2/Array.html and here
http://www.ruby-doc.org/core-2.1.2/Enumerable.html.

For example,
http://www.ruby-doc.org/core-2.1.2/Enumerable.html...
documents the max method used above.

I hope this helps.
614aa6cb0cc315b8ef993dea32679642?d=identicon&s=25 John Yd (abettertomorrow80)
on 2014-08-07 02:14
Thank you Dansei . You answered my problem. Basicallly I want to know
the name of the oldest monster in a group of 3 monsters. it like you in
a group of four people, i want to know the name of the oldest person and
his age. I can get the age but i cannot get the name . And your solution
fix the problem. thank u
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.