nemorathwald (
nemorathwald) wrote2009-02-01 09:14 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Dalmation Plantation Concatenation Notation
I and
le_bebna_kamni are sitting in Espresso Royale practicing programming in Ruby. We did the same exercise for making singular and plural sentences. When I looked at the way she did it in her version, I didn't recognize what the hash sign followed by curly braces is supposed to do. She explained it for a while. I summarized.
"So it's a concatenation notation."
"Yeah."
"For a dalmation plantation."
"It'll be a sensation!"
Breaking out into song
![[livejournal.com profile]](https://www.dreamwidth.org/img/external/lj-userinfo.gif)

"Yeah."
"For a dalmation plantation."
"It'll be a sensation!"
Breaking out into song
A concatenation notation,Then she made new code to demonstrate how it works in terms I would understand:
Where our population can roam
If this new location
Our whole aggregation
Will love our plantation home!
#!/usr/bin/ruby def pluralize(unit, quantity) if quantity == 1 then return "#{quantity} #{unit}" else return "#{quantity} #{unit}s" end end print "Concatenation for the Dalmation Plantation: ", \ pluralize("Dalmation", 101),"\n"Output:
Concatenation for the Dalmation Plantation: 101 Dalmations
no subject
The premise is that if something occurs twice in code, that's twice you have to remember to keep it updated when something changes. And by golly, it'll change. In this instance, observe how many times the format string appears in your function, versus in mine.
I also changed the argument order because code is poetry, it should read as you would speak it:
pluralize 101 dalmations
. This is known as the Principle of Least Surprise (http://c2.com/cgi/wiki?PrincipleOfLeastSurprise) because one day, maybe tomorrow, you're going to forget which way you defined those arguments ... was it dog then number? number then dog?It's never too early to start forming good habits.
(no subject)
(no subject)