Coder Social home page Coder Social logo

mod-1-discussion-questions-using-self-in-ruby's Introduction

self Discussion Questions

Instructions

Take 30 minutes and answer the following questions together with your group. Take turns playing around with the code provided in Pry or IRB.

Question 1

class FunnyBots
  attr_accessor :name, :quotes

  @@bots = []

  def initialize(name, quotes)
    @name = name
    @quotes = quotes
    @@bots << self
  end

  def random_quote
    self.quotes.sample
  end

  def self.bots
    @@bots
  end
end

quotes = [
  'Q: How did the programmer die in the shower? A: He read the shampoo bottle instructions: Lather. Rinse. Repeat.',
  "A UI is like a joke. If you have to explain it, it's not good.",
  'Q: How many programmers does it take to change a light bulb? A: None – It’s a hardware problem.,
]
archer = FunnyBots.new('Archer', quotes)
  • What is self in this line: @@bots << self ?
  • What is self in this line: self.quotes.sample?
  • What kind of method is self.bots and what is self?
  • Will this work: archer.bots? Why / why not?

Question 2

class Bicycle
  attr_reader :tire

  def initialize(tire, gears, style)
    @tire = tire
    @gears = gears
    @style = style
  end

  def tire_size
    self.tire
  end

  def self.gears
    @gears
  end
end

mongoose = Bicycle.new(4, 10, 'BMX')

For what reasons will the following method calls fail?

  • mongoose.tire_size = 5
  • mongoose.gears
  • Bicycle.bikes
  • Bicycle.styles

Restructure the class to fix the issues.

mod-1-discussion-questions-using-self-in-ruby's People

Contributors

antoinfive avatar drakeltheryuujin avatar graciemcguire avatar ihollander avatar maxwellbenton avatar robhitt avatar rrcobb avatar sgharms avatar sophiedebenedetto avatar sylwiavargas avatar telegraham avatar timothylevi avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mod-1-discussion-questions-using-self-in-ruby's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.