James Crisp

Software dev, tech, mind hacks and the occasional personal bit

ChatGPT Programming Test

I was writing a little function in Ruby and thought I’d ask ChatGPT to have a go at it.

It is easy to read, explicit, and fairly idiomatic Ruby (though not concise). Not the most performant implementation but nothing terrible. It also mainly works but does have a bug in some cases.

Here is the code:

def working_days_between(start_date, end_date)
  # Make sure the start date is before the end date
  start_date, end_date = end_date, start_date if start_date > end_date
  
  # Calculate the number of days between the two dates
  days = (end_date - start_date).to_i
  
  # Calculate the number of weekends between the two dates
  weekends = ((start_date..end_date).count { |date| date.saturday? || date.sunday? })
  
  # Subtract the weekends from the total number of days to get the number of working days
  working_days = days - weekends
  
  return working_days
end

If you have the start/end date on a weekend, then you get a negative answer. Eg,

working_days_between(Date.parse("Sat, 04 Mar 2023"), Date.parse("Sun, 05 March 2023"))
 => -1

It is because the weekend number of days calculation is including both the start date and the end date. Ie, working_days = 1 – 2 = -1

A human could easily have made the same mistake, mind you.

A better / simpler implementation is:

(from_date...to_date).count { |date| date.on_weekday? }

Note the 3 dots (…) for the date range, which does not include the end date.

Later, I tried asking ChatGPT to regenerate the answer multiple times. It gave me quite a different version every time – some versions with bugs, some with no functions, some with support for public holidays, etc.

OrthoK vs. Contacts vs. Glasses

A review of Ortho-K after 5 years, and a comparison with other options.

I spent the majority of my life with glasses, and generally they worked well except for water sports and fogging up (or getting knocked against my face when doing martial arts). For the last 5 years I’ve used OrthoK (Orthokeratology nighttime contact lenses) to correct my vision. Recently I’ve decided to give them a break and I’ve been using daily soft contact lenses.

OrthoK

+ Feel like naturally good vision when you are awake, no lenses to get dry, or get lost. No risk of you losing a lense and not being able to drive home.

+ Great for in water sports like surfing, boogie boarding and swimming without googles (pretty much the only option besides surgery or just not seeing well). Soft contacts have the risk of washing out in waves and also are not meant to be worn in these conditions as can get unclean water stuck behind the contact, leading to more chance of infection.

+ Good for pool swimming with googles, good for on water sports like sailing and kayaking (though need sun glasses).

+ Good for computer use (corrects astigmatism and don’t get dry eyes looking at screen like you can with soft contacts)

+ Cost effective compared to soft lenses. About $900 for a pair for 3-4 years, and say $120 of solutions every 3 months. That comes to about $740/year.

– + Change is long lasting. Takes about 1 month for effect to wear of completely (ie, can’t wear your old glasses for a long time). During this month, you need progressively changing glasses/contacts to correct your changing prescription and have to make do with the closest prescription you have on hand (can lead to headaches and poor vision). Not wearing for a night still leaves you with “good enough” vision for most things (except driving) but generally you have to wear every night (can’t just wear sometimes like soft contacts).

– Night-time vision is usually less good than daytime, so I tended to need to wear weak glasses for driving in the dark, and this tended to give me the edge of a headache sometimes. Also walking around in the dark in a place you didn’t know was harder though I usually didn’t bother with glasses for this.

– Variable vision ranging from perfect to OK and sometimes a bit poor, depending on how you slept / random variations in your eye condition.

– Slow to put in and out and clean. Budget about 5-10 minutes morning and evening (say average 15 min/day). Also monthly deep clean required, and you need to be very careful to wash hands well and use fresh towels to dry hands to avoid getting dirt in the lenses at all times.

– Occasionally get some dirt in the lense overnight and need to get up and take out and clean and re-insert and have poorer vision the next day.

– When travelling, always need to pack various solutions and backup glasses in case of problems. Hard to put in on an airplane so usually skip on overnight flights. Bit of a hassle on camping or multi-day sailing trips as need good lighting and water/soap in morning and evening, and not good for doing night watches on a boat where you need to get up in the night for your turn.

– Once you put the lenses in, you need to go to bed as not comfortable for extended use while awake. And when you get up, need access to bathroom right away to take them out. You can function in the middle of the night with lenses in if needed, but it is not that comfortable.

– Recently stopped OrthoK due to getting dry eye developing in one eye. This led to a sore eye and poor vision. With lots of expensive eye drops, my eye was not sore, but I was still finding it very hard to get good vision and putting in so many drops was a hassle. This problem only just started to happen this year, perhaps due to very dry winter air, and may not happen again if I resumed OrthoK later.

Glasses

+ Accurate eye correction for good vision every day.

+ Cheap, say $100-200 year assuming you keep your glasses for a few years.

+ Transitions allow automatic tinting like sunglasses in strong light.

+ Good for on water sports (with prescription sunglasses or transitions), but need band to stop them getting lost in water (eg, dinghy boat capsize).

+ Very fast to put on and take off (negligible!) and no hand washing required. Cleaning required daily or when get dirty but doesn’t take long.

+ Provides eye protection from sea spray, cooking with oil, etc

+- Highly visible on your face

– Peripheral vision is around the edge of the glasses and uncorrected on all sides.

– Easily gets dirty from rain/sea spray/accidental touches etc.

– No use for in water sports (though can get prescription goggles for swimming).

– Fogs up during exercise and when wearing a mask.

– Not good for martial arts as they tend to get knocked off or bent.

Soft Daily Contact Lenses

+ Fairly quick to put in and out (under 5 min a day) and need less hygiene than OrthoK. Easy to take with you when travelling.

+ Reliable vision correction that is still good at night and includes peripheral vision.

+ Can wear intermittently (eg, only some days based on activities planned).

+ Fine for on water sports, but still need sun glasses and a risk of losing a contact lense in dinghy boat capsize or similar.

+ Cost wise, not a big deal if you lose one.

– Not recommended for in water sports like surfing, boogie boarding and swimming without googles. Soft contacts have the risk of washing out in waves and also are not meant to be worn in these conditions as can get unclean water stuck behind the contact, leading to more chance of infection.

– Can get dry eyes in A/C (especially when driving) or using a computer (hasn’t been a big issue for me).

– Relatively expensive for daily use, about $1150 / year if used every day and bought 4 x 90 packs. Could be cheaper with weekly/fortnightly soft lenses.

– Makes quite a bit of rubbish since disposable.

At this point, I am still waiting for my OrthoK correction to fade and being able to try wearing my old glasses again. At the moment, I am using soft contact lenses which I am finding quite convenient, though it would be handy to have glasses of the right prescription to use as well (eg, when you get up). I will update this post when my OrthoK correction has worn off. I may resume my OrthoK in future (still have the lenses ready) but thought it was time for a review to see if they are worth the hassle and variable vision.

“4,000 Weeks” by Oliver Burkeman

When I heard about this book, I was intrigued by the idea of a time management book more about your lifetime rather than getting things done. I was not disappointed. Here are the take-aways I found the most interesting.

  • Often, you’re looking at your phone to escape an uncomfortable/boring situation.
  • If you focus totally on the current uncomfortable situation (accept you are there, and life is full of uncomfortable situations, rather than trying to escape) it will become bearable. As finite humans, we don’t get to dictate the course of events.
  • Having tricky problems means you’re alive, it’s the normal state. Life “is a process of engaging with problem after problem, giving each one the time it requires … the presence of problems in your life isn’t an impediment to a meaningful existence but the substance of one.”
  • Don’t wait for some future perfection when you get X, Y or Z or on top of things. There is always more to do, and things are always broken, and there will always be far more to do than you have time for. It is fine to neglect many things. Resign yourself to this finite reality. Serially choose one or a few things to focus on.
  • “Pay yourself first” – do what you really want to do first with your time each day, the rest will work out. Otherwise there won’t be time for what you want to do after doing the rest.
  • You don’t have time, your life is made up of time, “you are time”.
  • Making a decision/choice is liberating as then there is only one path forward. It is an affirmation – ie, you’ve chosen how to spend your time (no matter if it is earning money to support your family, playing with the kids, buying a house, or going hiking).
  • Don’t go with most comfortable option, think which grow or diminish you as a person.
  • If you want to feel like life is not going so fast as you get older, do more new things rather than follow routine – explore somewhere new, take a different route to work, take up a new hobby, etc.
  • “Attention is the beginning of devotion… you can’t truly love a partner or a child, dedicate yourself to a career or to a cause – or just savour the pleasure of a stroll in the park – except to the extent that you can hold your attention on the object of your devotion to being with.” — Mary Oliver
  • Notice you are already living in the moment anyway, like it or not. Trying self-consciously to “live in the moment” will fail.
  • Moments of bliss: enjoy a hobby where time passes without you noticing and you have no hope of achieving acclaim or profit. Freedom to pursue something you enjoy for no other reason.
  • Herzen: “Because children grow up, we think a child’s purpose is to grow up… but a child’s purpose is to be a child. Nature doesn’t disdain what only lives for a day… Life’s bounty is in its flow. Later is too late”.
  • Refuse to hold yourself to “an abstract and over-demanding standard of remarkableness.. drop back from godlike fantasies of cosmic significance into the experience of life as it concretely and finitely – and often enough, marvellously – really is.”
  • “No matter how much you plan or fret.. you can’t know that things will turn out all right” – there’s no real certainty so stop trying to manufature it.
  • A plan is “an expression of your current thoughts about how you’d ideally like to deploy your modest influence over the future. The future, of course, is under no obligation to comply”. Wonder what will happen next, rather than demand it to be as you want it to be.
  • You coming into existence at all, and most of your life is a highly unlikely series of events of which you had no control. So relax 🙂
  • You can put things in place to increase the chances of a happy outcome.. but there is no certainty in anything, or that things will turn out as you predicted.
  • When the uncontrollable future arrives we’ll have what it takes – we’ve got this far!
  • So “I don’t mind what happens.”

“How to Stop Worrying and Start Living” by Dale Carnegie

As a long-time fan of How to Win Friends and Influence People, I was excited read this book. Especially the early sections I found very interesting and useful. Some of the later sections feel dated and less relevant. The ideas I found most useful were:

  • If you are worried about something, write out:
    1. What am I worrying about?
    2. What is the worst that can possibly happen?
    3. [Accept it and imagine it happening.]
    4. What can I do about it?
    5. [Then do something about it right away.]
  • “When I am up against a tough situation, if I can do anything about it, I do it. If I can’t, I just forget it. I never worry about the future, because I know no man living can possibly figure out what is going to happen in the future.”
  • Check the facts, and come to a decision, then stick to it unless new facts come to light.
  • “God grant me the serenity to accept the things I cannot change; the courage to change the things I can; and the wisdom to know the difference.”
  • “Instead of worrying about ingratitude, let’s expect it.. the only way to find happiness is not to expect gratitude, but to give for the joy of giving.”
  • “Count your blessings, not your troubles!” — find out what is good in your present situation.
  • “Happiness is not mostly pleasure; it is mostly victory.”
  • “The really important thing is to profit from your losses. That requires intelligence.”
  • Do a good deed every day – ie, one that brings a smile of joy on the face of another.
  • Unjust criticism? Just laugh.
  • Ask for advice: “Won’t you please tell me what I did that was wrong when I tried to do XYZ? You are far more experienced and successful than I am. Please give me your criticism. Be frank. Don’t pull your punches.”
  • According to Henry Ford, how to increase energy and endurance: “I never stand up when I can sit down; and I never sit down when I can lie down”.
  • Relax muscles of face and eyes, relax the tension in your body (do it often) to have more energy.
  • Laugh at your sillier worries, you can laugh them out of existence.

Talk: Credit cards / Gateways

Tune in to the next Sydney ALT.NET meetup on Tuesday (30 Dec)! I’ll be giving a talk from around 6pm.

Accept credit cards: Gateways, architectures, code, and… money!

Have you been thinking to accept credit card payments for your new clever MVP, startup, or maybe even in your day job? Well, you’re in luck! James will give you a primer on how to do it simply and securely, based on his recent journey to the Gateway jungle.

Please RSVP on meetup and join the Twitch stream for some fun!

“Authentic Gravitas” by Rebecca Newton

I read this book a while back and have been meaning to write about it. I found it contained some interesting points, especially on being intentional (and comparing your actual impact with what you were aiming for) and working appropriately with different communication styles/personalities.

Here’s my favourite parts:

  • “The reality is, we all have a gap between our intention, our action, and our actual impact. To have gravitas, be clear about your convictions (what really matters in a given situation), be clear about your intention (how you want to show up and impact the situation and the people in it), be disciplined in checking that your impact is lining up with your intention (through your own observations and seeking feedback), and be committed to continually adapting your style.”
  • “On being intentional, ask yourself, What footprint do I want to leave very day? How would I want other people to describe me?
  • “People who are regarded as having high levels of authentic gravitas may look confident, but actually they are choose to be courageous.” “They feel fear and do it anyway. They choose courage.”
  • Ask more open questions in meetings to find out what is actually driving the other person’s thinking and decision-making. Eg, “What are you excited about right now”, “What’s your biggest concern?”, “What matters most to you at the moment?”, “How do you feel about what’s happening?”, “What is the main thing stopping you from making progress?”, “What are the forces at play in this issue?”.
  • If people ask for help, give them your full attention or ask them to come back later. Eg, “Yes, I’ve got 10 minutes – will that work?” “Or we can have a longer chat later today at 2pm?”
  • Script the opening of a talk carefully to engage and hold the room – verbally and nonverbally – from the outset before rushing into the details. Eg, open with “Thank you Angela. [pause] Good morning, all. It’s great to have you with us today. It’s a critical time in our industry. We’re currently facing uncertain market conditions, unexpected legislative changes and rapid technological development. Navigating this environment seems more challenging than ever. I’d like to share with you…”. Script the finish also. The middle can be looser. Telling personal stories helps connect with the audience.
  • IMPACT preparation for a meeting: What do you believe about this situation/possibility? What do you want them (the people you engage with) to think, feel and potentially act differently as a result of this encounter with you? What is motivating them (acquire, bond, comprehend, or defend)? What is their perception of this situation and of you right now (and what do you want it to be)? If nothing else, what would you want them to remember and pass on (max 3)? How should you open and close? What stories would be relevant and support your message? And finally, think about your technique.
  • Avoid the “face freeze”: Put your hands into fists and roll your knuckles in circles around and around in your cheeks. It loosens your face and you become more conscious of your facial muscles, enabling you to feel the tension in them and make the choice to relax them.
  • Even if people don’t give you positive non-verbal/verbal affirmation, “Choose to give everyone equal energy and attention.. [remember they have a gap between intention and impact too]”. With big audience, look at a back corner of the room and slowly use your eyes to follow an “S” pattern down through the group to the front row. After a minute, go back up to the opposite corner and find down again following an inverted “S”. In a small meeting, be mindful from the outset of your decision to give everyone eye contact.
  • Opportunity mind-set rather than a threat mind-set. You’re most likely nervous because this situation matters. And it matters because it’s an opportunity that could lead to positive outcomes. Don’t get anxious, get excited!
  • “When working with people who tend to be optimistic and big-picture oriented, it’s important to respond with energy if they are sharing an idea. Explain why you like the ideas (be authentic and only communicate what you genuinely feel positively about), or comment on how much work they’ve clearly put into it, or the interesting nature of the project or topic. And share that with energy.. not over the top.. but at least some energy.” Immediately critiquing a new idea will deflate them and close their ears. Instead, after initial positively, ask something like “Would it help if I went through the technical details and came up with a list of things for us to look at? I want to make sure it’s all smooth when you take it to market.”
  • Prepare a short causal response to the “How are you?” type question in case of casual coffee machine conversations at work. Eg, “Oh, hi Chip. I’m well thanks. We’re busy with a new project around sustainability. We think this can really drive some positive change. We’re seeing a lot of interest from clients. How are things with you?”. Make brief encounters matter.
  • Remember the wide variety of influencing techniques available: rational persuasion, legitimating (because X says so), favours and exchange (Can I ask you a favour?), inspirational appeals (emotions and values), ingratiation (I don’t how to do it!! reply: I know you can. You you’ll be good at this because [reasons why]) and consulting (ask their help/feedback to increase buy-in).

Lockdown with Kids – Take 2

It’s coming up to the second month of lockdown in Sydney with my energetic primary school age sons and lovely (and patient) wife. Since this is the second time around, I thought I’d share some ideas which we have been using which have worked for us. Your mileage my vary!

First of all, let us reframe the lockdown as an epic cruise through space-time, towards normal life.

With this framing in mind, think about the entertainments and events and plan for a long voyage on a ship, to stave off boredom and keep the crew happy and productive.

  • Decide and family routine together and publish it on the fridge. Stick to it to give structure to the day. Make sure it includes recess and lunch similar to school – these are also when the family gets together from their separate activities during the work/school day.
  • Have a guessing competition on the number of covid cases for the day (new cases & number in community) and a prize draw at recess (eg, a chocolate for the winner). Everyone records their guess at breakfast.
  • Make use of every room in the house, balconies, porches, gardens etc to give more opportunity for privacy and space.
  • Celebrate the end of the work/school week by ordering delivery food for dinner on Friday. This delineates the week from the weekend, and gives parents a break from cooking and everyone something to look forward to.
  • Have international food nights once a week: choose a few countries for the first few, then when you run out, play pin the tail on the country with a map! Research the food, order appropriate ingredients and cook. Dress up if you feel like it. Make it a highlight of the week.
  • Family kareoke nights if that’s your thing. I think dancing could work too though we haven’t tried it yet.
  • Get kids involved in cooking whenever you can, they love it and learn too.
  • Get lots of outdoor activity gear such as basket ball hoops, soccer balls, pull up bars, bikes, kites, etc to provide variety.
  • Exercise every day to get out the the house (and usual exercise benefits). Choose a mix of quiet activity by yourself to get some space (eg, running) and with kids (eg, bicycling) and go with what you feel like on the day. Calisthenics is good in a park where kids can play while you work out.
  • Make computer game playing time for kids a currency that they earn. For example, our kids earn:
    • 50% of the time they spend on extra study outside of school for game playing.
    • I review their school work at the end of the day and award game time based on amount and quality of work they have done during the day (in range 15 min – 1 hour).
    • Chores like taking out the garbage/compost earn 5 minutes.
    • Repeatedly doing something we’ve asked them not to do (or not doing something they are meant to do) leads to a fine of 1 or 5 minutes of game time.
    • Ask kids to do a written accounting of their time before they use their game time, to check the amount of time and to give them practice presenting tabular data and doing calculations.
  • Remember that work done at home by the kids for home schooling does not need to be perfect and fixed by parents before submission. The teacher can correct kids’ work during the day. Sure, there are times when the boys need help, and that is OK too, but you don’t need to sit with them every minute. Home schooling does give you the possibility of getting more involved yourself, but I like to have the boys work as independently as possible during the day and go over things with them after school “finishes”. This also allows me to get more of my work done during the day.
  • For school work, if possible, ensure each child has their own device with a camera for submitting hand-written work, to avoid having lots of logging in and out to switch accounts, and fights over the device.
  • Make an effort to stay in touch with friends via video calls (at least one call a week).
  • Get good noise cancellation headphones for when you are working. For me this is vital.

I hope you find some of these ideas useful as we cruise on towards freedom.

“The Visual MBA” by Jason Barron

I’ve been wondering for a while what you learn from a MBA. When I came across “The Visual MBA” by Jason Barron, promising “A quick guide to everything you’ll learn in two years of Business School”, I thought it’d be worth the time to have a read and find out.

The book is as promised, visual. On each topic it is about 50/50 writing and relevant diagram. It was based on the author’s notes doing his MBA. The book is generally easy to understand – there’s a few places where I wanted a bit more depth to understand the message, but in most places it was sufficient to communicate the concept.

Here are the most memorable points from the book for me:

  • The value of money now vs. the value of the promise/prediction of money in the future, and a way to calculate it using the Present Value formula. It can be used to work out how much money you would need now to equal an amount in the future, or to take the risk into account as a discounting rate on a future prediction.
  • Who are your customers and who are not? You can’t build a product to suit everyone as it will suit nobody well. Instead segment and target the most valuable potential customers and build a relevant, exception product for them.
  • Laddering – what sells is not the product features, it is the personal value. “Ask your biggest fans what they like (a particular feature), why they like it (product benefit), why that matters (personal benefit) and how that connects to a high level personal value”. Eg, fast car – feature: speed, product benefit: get places quickly, personal benefit: makes me feel young, personal value: youthfulness. “Position your marketing materials through the love group’s eyes while targeting the swing group to gain new customers.. Don’t waste time on the haters.” Sale is usually based on emotion evoked (eg, makes me feel cool, youthful etc) rather than the features.
  • When branding, think about what we do (eg, “we bring kids happiness”), then how we do it (“by making toys”) and then why we do it (“because children are our future and each one deserves to smile in a darkening world”). “People care about the WHY we do what we do… that becomes our brand mantra…” and should be used to guide decisions.
  • For people in general, “logic is like a man riding an elephant (emotion). Guess who decides where to go?”. Appeal to peoples’ emotions.
  • “How can we get young people signing up… why? .. because our revenue is going down.. OK maybe the right question should be why is our revenue going down?”. Make an issue tree (like a decision tree on why the problem is happening) and test each branch to see if it is the cause.
  • Advice from Churchill: “Success is going from failure to failure without loss of enthusiasm”! Ponder then act decisively.

“Selling the Invisible” by Harry Beckwith

Billed as “a field guide to modern marketing”, I found this book a highly interesting read, especially the second half. It is a guide to marketing services (“the invisible”). Some of the stand outs points for me were:

  • “Forget looking like the superior choice. Make yourself an excellent choice. Then eliminate anything that might make you a bad choice.” Ie, address any risks in peoples’ minds, rather than trying to show you are better than all other options. It is very hard to tell if a service was the best, but it is easy to tell if it was bad. Eliminate the fear.
  • “Setting your price is like setting a screw. A little resistance is a good sign.” Beckwith argues that 10% of people will always complain about the price. Resistance in about 10% more (total 20%) is about right. “When it [resistance] starts exceeding 25%, scale back.”
  • Often you are competing against apathy (doing nothing) or the person themselves doing the work.. not another firm.
  • “Don’t assume that logical pricing is smart pricing. Maybe your price, which makes you like like a good value, actually makes you look second-rate”.
  • “The premium service and the low-cost provider occupy nice niches all by themselves. If you are priced in between, however, you are competing with almost everyone”.
  • “Don’t start by positioning your service. Instead leverage the position you have.” eg, Avis ads repeated for years “We’re Number Two. We try harder.”
  • “In positioning, don’t try to hide your small size. Make it work by stressing its advantages, such as responsiveness and individual attention.”
  • “If you’re selling a service, you’re selling a relationship.. before you try to satisfy ‘the client’, understand and satisfy the person.. In large part, service marketing is a popularity content.. be personable.”
  • “A brand is more than a symbol. In the public’s eye, a brand is a warranty. It is a promise that the service carrying that brand will live up to its name, and perform.. a service is a promise, and building a brand builds your promise.. Invest in and religiously preach integrity. It is the heart of your brand… Brands are decision-making shortcuts [for busy people].. Make selling easier, faster and cheaper. Build a brand… A brand is money.”
  • “Selling a service involved personal risks [rejection etc].. risk yourself.”
  • “Saying many things usually communicates nothing.. Meet your market’s very first need: Give it one good reason… After you say one thing, repeat it again and again. Watch and perfect the visual clues you send.. repeat yourself visually, too.. Give your marketing a human face.”
  • Be genuine and honest, show your passion.
  • “Take advantage of the Recency Effect. Follow up brilliantly.”
  • “Execute passionately. Marginal tactics executed passionately almost always will outperform brilliant tactics executed marginally.”
  • “Above all, sell hope.”
  • “Your parents were right. Say thank you. Often.”

Rails ActiveModel, with nested objects and validation

So maybe you have a model that is not backed by a database table? ActiveModel is meant to cover this scenario and give you the usual ActiveRecord goodness and validation.

But the story gets much harder if you want to have nested objects. In a normal ActiveRecord::Base backed model, you can simply say:

accepts_nested_attributes_for :order_lines

and Rails will manage form submitted nested parameters for you.

Life is not so simple, or documented with nested objects on ActiveModel. accepts_nested_attributes_for is not available. But some of the underpinnings are.

So enough talk, how do you make it work? I’ll show you with an Order / OrderLines example.

Note the very special name: order_lines_attributes=. This hooks into the Rails handling of nested form parameters. Also the valid? method propagates the child errors up to the parent object, so that they show up at the top of the form.

Now how do you do the nested form? It’s similar to normal database backed nested records.

Hope this helps, it is not documented anywhere I could find, and worked out mainly though reading the Rails source.

Page 1 of 20

Powered by WordPress & Theme by Anders Norén