Today I Learned

4 posts by ryanmagowan

A little thing about .to_str

Playing with Ruby's === today and found some knowledge that's share-worthy. I noticed in ruby docs for string === a reference to .to_str and decided to investigate.

Nothing too exciting here, but its an important point of reference.

> hello = "hello"
> goodbye = "goodbye"

> hello === hello     #=> true
> hello === goodbye   #=> false

This is also what would usually be expected. Hang in there...

> string = "string"
> object =
> string + object     #=> TypeError no implicit conversion

Here's where things get funky.

class SomeObjectWithToStr
  def to_str
    "is now a string"

> string = "string"
> object =

> string + object     #=> "string is now a string"
> "string is now a string" === "string" + object      #=> true

Hunh? Why did that work?

TIL that .to_str is the default method call when operators force a conversion to a string. You'll likely have to define it yourself. Also note that the object type on the left is what the object type on the right will try to convert into.

Do you know of any Objects that come with pre-defined .to_str methods?

Database Column "type" is reserved in Rails

I made a database column called type. It contained a string. It was bound to Object. Everything was cool.

I wanted to create my object. But I kept getting this error.

object = Object.create(food: "P&L Burger", name: "The Matty", type: "Burgers")

Invalid single-table inheritance type: 1 is not a subclass of Object

TIL that Rails has some reserved keywords for database column names. It just so happens that the column name type is reserved. There are also other reserved column names.

See: Active Record Basics: 2.2 Schema Conventions

The type column is reserved for single-table-inheritance where the Ruby Object name would be stored in the type column and would be accessible through object.type #=> Object

See: Single Table Inheritance

In RubyMine, shift a line or a whole block of code

RubyMine has a key-mapping for grabbing lines of text in the editor, from cursor focus. If you have your cursor focused anywhere in a line of code you can shift it up or down with the following [default] key bindings:

CMD + SHIFT + Up Arrow || Down Arrow

Supercharge your grabbing
If you want to move an entire block of code without having to highlight the entire block, put your cursor on the definition of the statement and RubyMine will shift the entire block for you.

This can work on blocks defined by module; class; def; if; find more use cases and share because I definitely haven't uncovered them all yet!

Happy Shifting!

Rails console options to make your life better

Often when learning or testing out an implementation in planning, entering into a REPL to get some feedback and play with something more physical is desirable.

Here are some tips to improve your rails console workflow:

Specify the environment you wish to use

Maybe you want to try and reproduce an error in production during your test runs. Maybe you want to seed the database for your development server before having created a seed file. Maybe you're just feeling wild for the day and want to switch things up.

Rails gives you the option to select the environment for the console session:

>rails c --environment=test [test/development/production/...other...]
>rails c test  # as a shorthand

Note: The default environment is set to development

Sandbox so your DB entries do not persist

Often I find myself wanting to just play around with the current build, knowingly wanting to throw away the changes after. Sandboxing is a great way to do this while reducing your cleanup workflow.

>rails c --sandbox
>rails c -s

This saves you from having to write out the following to return to a seeded state:

>rake db:reset RAILS_ENV=[environment]