What did you learn today?

New post

TIL in Ruby: `arity`

by @rinas

Returns the number of mandatory arguments.
If the block is declared to take no arguments, returns 0.
If the block is known to take exactly n arguments, returns n.
If the block has optional arguments, returns -n-1


https://apidock.com/ruby/Proc/arity
Screenshot 2022-10-09 at 2.00.31 PM.png 957 KB

[Ruby][Rails] Learned about a new gem decent_exposure

by @rinas

https://github.com/hashrocket/decent_exposure

Screenshot 2022-09-14 at 7.53.55 PM.png 1.1 MB

TIL what bare git repositories and git worktrees are

by @ilango

Git has this feature called bare repositories. After a normal clone, along with our project files, there's a `.git` hidden directory. Going into the .git directory, we can see all the folders and files that git keeps to do our version control.
bare-repo.png 7.46 KB

A bare repo, doesn't have our project files and only has the folders and files in the above image. When we clone a repo, if we add `--bare` to the clone command, we make a bare repo.

Why do this?
For one, we can clone off of a bare repo:

git clone --bare /home/ilango/aurelius aurelius_2

Note that the I'm cloning a repo from a local folder. This is useful for collaboration when we have a shared storage in a local network and don't want to host our project in the cloud.

What made me more interested in bare repos is worktrees. Git worktrees are one of the lesser known but coolest features. It allows us to have unfinished work in multiple branches at the same time, without having to use `git stash`.

For example, let's say you're in `feature-branch-1`, and are made aware of a bug in `main`. You have unstaged work in feature-branch-1, so you run `git stash`, checkout to main, fix the bug, come back to the feature branch and run `git stash pop`. With worktrees, we don't need to do all this.

Instead of a normal clone, do a bare clone,  and run

git worktree add main main

Note: first "main" is the folder name, second "main" is the branch name. Also note, the main folder in the image. That's my main branch worktree.

This creates a main folder in your bare repo. Changing to this repo we can see that the main folder contains our main branch! We can do this for as many branches as we want and switch between them with different changes. We don't need bare repositories for using git worktrees but it keeps my folder structure cleaner.

More details here: https://git-scm.com/docs/git-worktree

GitHub has a "template repository" feature

by @mzrnsh

This allows users to create fresh repositories based on the template repository, without creating forks, or keeping the git history. Handy for boilerplates or starter projects.

It's located right under the project name in settings, and once on, a new green button "Use this template" appears on repository home page. You can see it in action on my Jekyll+Tailwind boilerplate: https://github.com/mzrnsh/jekyllwind

TIL about PostgreSQL follower database in Heroku

by @rinas

TIL in Golang: Identifying Isograms & difference of squares

by @ilango

I did a couple of simple exercises on Exercism — identifying isograms and difference of squares.

After I finished them, I checked the community solutions and realized how brute-forced, inefficient my solution was. But that's the process of learning.

By checking community solutions, I learned about two methods available in the standard library: IsLetter and ContainsRune!

TIL in Golang: Time, Type Conversion & Assertion, First class functions

by @ilango

Time package in Golang is, honestly, the most pain-in-the-ass, annoying shit ever. I did NOT have a good time debugging issues in the exercise. The other ones were pretty clear, mostly. Type Assertion also made me pull my hair out a bit, but not as much as time. xD

Fortunately, time was the last concept I had to learn in the Go syllabus on Exercism. Now on to the other exercises, 1 or 2 a day. Also, it's time to dive into some projects from this project-based learning repo: https://github.com/practical-tutorials/project-based-learning#go

*SPOILERS*
Time: https://exercism.org/tracks/go/exercises/booking-up-for-beauty/solutions/ilango
Type Conversion & Assertion: https://exercism.org/tracks/go/exercises/sorting-room/solutions/ilango
First class functions: https://exercism.org/tracks/go/exercises/expenses/solutions/ilango

TIL in Flutter, we can specify the app version and build number in pubspec.yaml

by @rinas

I was changing the marketing version and build number manually in Xcode but looks like it can be controlled from pubspec.yaml file which is very handy
Screenshot 2022-05-07 at 7.58.47 AM.png 250 KB

TIL we can use `xed` to open Xcode with given folder or project

by @rinas

Screenshot 2022-05-07 at 7.53.09 AM.png 240 KB

TIL in Golang: Interfaces & Errors

by @ilango

This one was a bit hard to get my head around. There was one common exercise for both of these concepts. But while working on the exercise itself, I realized how important these concepts were, especially if I'm working on projects or public packages.

So there is no exceptions for error handling in Go. Instead errors are values of an interface called `error`. In order to learn errors, I had to learn interfaces.

I still need more practice on these two topics because I feel my understanding of them is a bit shaky. Anyways, here's my solution:

*SPOILERS*
https://exercism.org/tracks/go/exercises/the-farm/solutions/ilango

TIL in Javascript, Date `getMonth()` does not give the correct month as we expect 🤯

by @rinas

today = new Date()
Thu May 05 2022 19:28:34 GMT+0530 (India Standard Time)
today.getMonth()
4

what! it should be 5 (May!)

looks like the return values are from 0 to 11

so for now we'll have to do today.getMonth() + 1 to get correct month value


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth#return_value

Netlify serves Jekyll 404 pages out of box

by @mzrnsh

I have built many Jekyll websites, but I kind of always forgot to add a 404 page. I knew it works out of box on local server (Webrick) and GH Pages.

Well, today I learned that Netlify respects our time as well and placing a `404.html` page in the root will also work magically over there, with 0 config.

TIL in Golang: Regular Expressions

by @ilango

I don't like working with regular expressions much although I've written a fair share of them. But today's Go lesson on regular expressions is the most fun I've had with them.

The exercise is parsing logs and cleaning up logs. We have to write a bunch of functions that does different cleaning up and organizing tasks.

*SPOILERS*
Solution: https://exercism.org/tracks/go/exercises/parsing-log-files/solutions/lbcosta

TIL in Golang: Methods & Runes

by @ilango

Today's lessons were Methods and Runes.

Pretty simple exercises but took me a bit of time to figure out a few issues, especially in runes.

*SPOILERS*
Runes: https://exercism.org/tracks/go/exercises/logs-logs-logs/solutions/ilango
Methods: https://exercism.org/tracks/go/exercises/elons-toys/solutions/ilango

TIL in Golang: Pointers

by @ilango

I haven't done anything with pointers since my C++ days and that was years back.

Anyways, today's Golang lesson was Pointers. The exercise was to build a simplified digital vote counting system for a students' association president election.

Here's my solution (SPOILERS): https://exercism.org/tracks/go/exercises/election-day/solutions/ilango

TIL in Golang: Range Iteration & Type Definitions

by @ilango

Going through the Go track syllabus on exercism.org.

Today's lessons were Range Iteration and Type Definitions. Both have a common exercise, which was to generate a simple chessboard and do small calculations on it.

Here's my solution (SPOILERS): https://exercism.org/tracks/go/exercises/chessboard/solutions/ilango

TIL we can compare versions in Ruby using `Gem::Version` without any gems

by @rinas

We can just do 

Gem::Version.new('1.0.1') > Gem::Version.new('1.0.0')

How to add images to a README.md file

by @alahirajeffrey

To add an image to a readme.md file, all you have to do is ![alt text](link to picture)

SimpleHTTPServer has become even simpler in Python 3

by @mzrnsh

One of the simplest ways to serve a static website on locally has forever been opening up a terminal window, "cd"-ing to the website folder and running Python's SimpleHTTPServer in it:

python -m SimpleHTTPServer 8000 

Today I learned that the command has become a bit more memorable in Python 3:

python3 -m http.server




TIL in Flutter (iOS): No transparent images allowed in assets

by @rinas

twitter_FP9neCQWYAQjImv.jpg 136 KB

TIL How to change screenshot directory in macOS

by @rinas

defaults write com.apple.screencapture location {location here}

e.g, 

defaults write com.apple.screencapture location /Users/rinas/Desktop/Screenshots

Heroku cli for rails console

by @rinas

heroku run rails c -a APP_NAME

TIL in Rails: Code after redirect_to gets executed

by @rinas

Since it gets executed, we need to add `return` 

redirect_to url and return

Here is the issue which talks about this: https://github.com/rails/rails/issues/26363

TIL about using counter_cache in rails to save association's count in parent model

by @rinas

I had used counter_cache in the past but still had to look it up :D

Here is the SO answer that helped: https://stackoverflow.com/a/16996960/3098242

-

TIL about beginless and endless ranges in Ruby

by @joemasilotti

TIL about beginless/endless ranges in Ruby 2.7 which can be used to clean up Active Record queries!

# Users with an ID less than or equal to 200.
User.where("id <= ?", 200)
User.where(id: ..200)

# Users with an ID less than 200.
User.where("id < ?", 200)
User.where(id: ...200)

# Users with an ID greater than or equal to 200.
User.where("id >= ?", 200)
User.where(id: 200..)

TIL how to select values from one column without `nil` in Rails

by @rinas

Update:

There is a better way to do this as discussed here in Twitter

Page.where.not(custom_domain: nil).pluck(:custom_domain)


---------------------------------------

Page.pluck(:custom_domain)
Screenshot of rails console

`.pluck` picks values but includes `nil` as well. So we can use `.compact`


Page.pluck(:custom_domain).compact
Screenshot of rails console

TIL How to change progress bar colour in Rails

by @rinas

.turbo-progress-bar {
  background-color: black;
}

You can see it in action here: https://whatsnew.co/

TIL how to set default ruby version using RVM

by @rinas

rvm --default use 3.1.0

TIL what policy to use for AWS S3 to work with Rails Active Storage

by @rinas

TIL how to colour console logs

by @rinas

We can change the colour of log in console like this:

console.log('%c hey red text', 'color: red')
console.log('%c hey green text', 'color: green')

Screenshot of developer console



Regarding privacy concerns, it's simple - we don't sell your data. In fact, we try to use privacy-focused software/services like Fathom Analytics whenever we use any third-party services.