Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Help for some reason my class isnt responding to variable

class Pokemon
attr_reader :name, :type, :attacks, :hp

def initialize(name, type, attacks, hp)
@name = name
@type = type
@attacks = {}
end

def typee
case type

when "electric" then puts "#{name} is a electric pokemon. Electric pokemon are weak to Ground and grass type pokemon. Electric attacks are super effective to water and flying types"
when "water" then puts " #{name} is a water type pokemon. Water types are weak to electric and grass type pokemon. All water types attacks are super effective to fire and ground types."
when "grass" then puts "#{name} is a grass type pokemon. Grass type pokemon are weak to fire and ice attacks. There attacks are super effective against electric and water types."
when "fire" then puts "#{name} is a fire type pokemon. Fire types are weak to water and ground types. There attacks are super effective to grass and fighting types."
when "fighting" then puts "#{name} is a fighting pokemon and is weak to psychic. There attacks are super effective against flying types."
when "rock" then puts"#{name} is weak to water attacks. Rock type attacks are super effective against fighting types."
when "dragon" then puts "#{name} is a dragon type pokemon. Dragon types are weak to ice type attacks. Dragon attacks are super effective against other dragons."
end
end

def teach_move_set

move_set1 = {fire_blast:5, flamethrower:4, overheat:7, ember:3}
move_set2 = {water_gun:3, hydro_pump:5, bubblebeam:3, water_pulse:4}
move_set3 = {thunder:6, thunderwave:0, thunderbolt:4, zap_cannon:5}
move_set4 = {tackle:1, headbutt:2, metronome:1, doubleslap:2}
move_set5 = {solarbeam:6, razorleaf:4,vinewhip:3, leechseed:1}
move_set6 = {megapunch:5,flinch:1,counter:3,firepunch:3}
move_set7 = {dragonrage:4, dragonpulse:4,crunch:5, outrage:5}

if type == "electric"
move_set3.merge(attacks)
elsif type == "fire"
move_set1.merge([attacks])
elsif type == "water"
move_set2.push([attacks])
elsif type == "grass"
move_set5.push([attacks])
elsif type == "fighting"
move_set6.push([attacks])
elsif type == "dragon"
move_set7.push([attacks])
elsif type == "normal"
move_set4.push([attacks])

end
end


end


pikachu = Pokemon.new("pikachu","electric","",90)

pikachu.type
pikachu.teach_move_set

I've made a start for you but please sort out the indentation and line breaks etc

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Hey man sorry for the confusion if it don't look right. But the indentation looks just fine for me . Can you show me how it should be indented? i Mean i dont think it came out right for you.

Hey Alphonse, just try to make it look like it would in the your text editor. I assume you use indentation there, no?

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

But i did do this in my editor. =( I just copy and pasted from the editor into treehouse.

Ah, well there's quite a lot wrong with the indentation, to be honest - maybe just check out some of the fundamental Ruby tutorials - I think they probably cover indentation somewhere. I'll fix it up for you. Probably the most fundamental rule of indentation is that if something is 'inside' something, it should be indented relative to its 'parent' eg the contents of a method should be indented relative to def and end.

Ah, I can't fix it because I'm not a moderator anymore! I'll copy/paste instead.

1 Answer

class Pokemon
  attr_reader :name, :type, :attacks, :hp

  def initialize(name, type, attacks, hp)
    @name = name
    @type = type
    @attacks = {}
  end

  def typee
    case type
    when "electric" then 
      puts "#{name} is a electric pokemon. Electric pokemon are weak to Ground and grass type pokemon. Electric attacks are super effective to water and flying types"
    when "water" then 
      puts " #{name} is a water type pokemon. Water types are weak to electric and grass type pokemon. All water types attacks are super effective to fire and ground types."
    when "grass" then 
      puts "#{name} is a grass type pokemon. Grass type pokemon are weak to fire and ice attacks. There attacks are super effective against electric and water types."
    when "fire" then 
      puts "#{name} is a fire type pokemon. Fire types are weak to water and ground types. There attacks are super effective to grass and fighting types."
    when "fighting" then 
      puts "#{name} is a fighting pokemon and is weak to psychic. There attacks are super effective against flying types."
    when "rock" then 
      puts"#{name} is weak to water attacks. Rock type attacks are super effective against fighting types."
    when "dragon" then 
      puts "#{name} is a dragon type pokemon. Dragon types are weak to ice type attacks. Dragon attacks are super effective against other dragons."
  end
end

def teach_move_set
  move_set1 = { fire_blast: 5, flamethrower: 4, overheat: 7, ember: 3 }
  move_set2 = { water_gun: 3, hydro_pump: 5, bubblebeam: 3, water_pulse: 4 }
  move_set3 = { thunder: 6, thunderwave: 0, thunderbolt: 4, zap_cannon: 5 }
  move_set4 = { tackle: 1, headbutt: 2, metronome: 1, doubleslap: 2 }
  move_set5 = { solarbeam: 6, razorleaf: 4, vinewhip: 3, leechseed: 1 }
  move_set6 = { megapunch: 5, flinch: 1, counter: 3, firepunch: 3 }
  move_set7 = { dragonrage: 4, dragonpulse: 4, crunch: 5, outrage: 5 }

  if type == "electric"
    move_set3.merge(attacks)
  elsif type == "fire"
    move_set1.merge([attacks])
  elsif type == "water"
    move_set2.push([attacks])
  elsif type == "grass"
    move_set5.push([attacks])
  elsif type == "fighting"
    move_set6.push([attacks])
  elsif type == "dragon"
    move_set7.push([attacks])
  elsif type == "normal"
    move_set4.push([attacks])
  end
end


end


pikachu = Pokemon.new("pikachu", "electric", "", 90)

pikachu.type
pikachu.teach_move_set

This is a good example of how indentation makes things much easier to read and understand - can you spot the problem? Check out where your class and methods start and end.... :-)