Today I Learned

9 posts by mateuszwieczorek

Ordering by SELECT index

Usually, you will order your SQL results by column name for example:

SELECT id, first_name, last_name
FROM users
ORDER BY last_name

but you can achive simillar result by replacing last_name by the index position in SELECT part of the query

SELECT id, first_name, last_name
FROM users
ORDER BY 3

NOTE: index starts with 1 instead of 0

Persisting custom methods in ruby ​​interpreter

To enhance your interpreter you may add some custom method or functions to your .irbrc or .pryrc files that usually can be found in your home directory ~/.irbrc, ~/.pryrc

How example ~/.pryrc file may look like:

def v_trace
  caller.select { |x| x.include?(Rails.root.to_s) }
end

class Object
  def m?(method_name)
    self.methods.grep /#{method_name}/
  end
end

After saving the above changes, run pry command and take advantage of new features

NOTE:

Extracting substring using regexp

If you want to grab some part of the string using a regular expression there is a high chance that you are using match or scan method

pry(main)> '$$$ Tony Stark $$$'.scan(/\w+ \w+/)[0] # -> Tony Stark

pry(main)> '$$$ Tony Stark $$$'.match(/(\w+ \w+)/)[1] # -> Tony Stark

next time try this

pry(main)> '$$$ Tony Stark $$$'[/(\w+ \w+)/] # -> Tony Stark

More advanced examples

pry(main)> '$$$ Tony Stark $$$'[/\${3}\s(\w+\s\w+)\s\${3}/, 1] # -> Tony Stark

pry(main)> '$$$ Tony Stark $$$'[/\${3}\s(?<name>\w+\s\w+)\s\${3}/, :name] # -> Tony Stark

as always visit www.crystular.org to validate your regexp

Creating array without parentheses

One of the unique features in ruby is that you don’t need to use parentheses(most of the time). For example, you can skip them when you are defining or invoking a method.

What I didn’t know was that you don’t need brackets while creating a new array

pry(main)> new_array = 1, 2, 3 # => [1, 2, 3]

More examples:

pry(main)> new_array = 1, second = 2, third = 3 # => new_array = [1, 2, 3]; second = 2; third = 3
pry(main)> new_array = first = 1, second = 2, third = 3 # => new_array = [1, 2, 3]; first = 1; second = 2; third = 3

Iterm triggers

Iterm terminal has a cool feature called Trigger that can be found in Preferences/Profiles/Advances/Triggers. What it allows you to do is to perform some actions when a certain text is displayed in your terminal panel.

For example you can highlight line that contains word Error so you won’t miss it in your dev logs.

setup usage

NOTE: Keep in mind that highlighting is only one of many actions that you can take.

SQLite3::RangeException: column index out of range

If you encounter the error:

SQLite3::RangeException: column index out of range

one of the things you might do is to investigate the broken query (joins/includes) and check if there is any association with limit condition like in the example below:

has_one :current_term, -> { order(created_at: :desc).limit(1) }, class_name: "Term"

the solution is to remove limit

has_one :current_term, -> { order(created_at: :desc) }, class_name: "Term"