Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:03:02+00:00 2026-06-06T19:03:02+00:00

tl:dr how does decoupling work? could need some little example I’m reading Programming Ruby

  • 0

tl:dr how does decoupling work? could need some little example

I’m reading Programming Ruby – The Pragmatic Programmer’s Guide.
(http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html)

There is an example on how to implement to_s for subclass KaraokeSong of Song.

class KaraokeSong < Song
# ...
def to_s
"KS: #{@name}--#{@artist} (#{@duration}) [#{@lyrics}]"
end
end
aSong = KaraokeSong.new("My Way", "Sinatra", 225, "And now, the...")
aSong.to_s    »  "KS: My Way--Sinatra (225) [And now, the...]"

Now they say its a bad way to do it:

Say we decided to change Song to store the duration in milliseconds. Suddenly, KaraokeSong would start reporting ridiculous values. The idea of a karaoke version of “My Way” that lasts for 3750 minutes is just too frightening to consider.

Instead you should define to_s with super:

def to_s
super + " [#{@lyrics}]"
end

Now when the @duration variable still stores the song duration in miliseconds, how does the new to_s which just calls the parent’s method solve the problem? It still returns 3750 minutes, doesn’t it?

I think i dont really understand the difference between those 2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:03:04+00:00Added an answer on June 6, 2026 at 7:03 pm

    It’s assumed that Song will take care of the proper output of its @duration.

    And if we will decide to change Song to store the duration in milliseconds, we will just change Song#to_s method and won’t have to change to_s of all its descendants.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does the G'MIC (Grey's Magic Image Converter) library work on iOS ? what steps
Does anyone have a working, step-by-step example of how to implement IEnumerable and IEnumerator
Does anyone know is there a better way to check if some image contain
does Mercurial have an HTTP protocol we could browse files/folders/branches instead of clone/pull changesets?
I absolutely need to use an IoC container for decoupling dependencies in an ever
Does anyone have a code example that follows best practices of using MongoDB Official
Does anyone know what this error means? I've read that it could be PHP
Does anyone have experience / insight re: decoupling jquery / sizzle? this is for
I'm reading theory about dependency inversion and decoupling and I can't see the difference
Does anyone know if there is a way to generate different code in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.