For many years, Ruby on Rails has been the go-to framework for startups, micropreneurs, SMBs, and really for anyone who needed to build and launch a tool quickly. With its opinionated, convention over configuration approach, and Heroku’s brilliant git push to deploy feature, Ruby on Rails made every other solution look barbaric by comparison. But … Continue reading Embracing Change: Rails 5.1 Adopts Yarn, Webpack, and the JS Ecosystem
Computing
A short summary of centrality in graph theory
A short summary of mathematics of networks (in particular, centrality). In graph theory and network analysis, indicators of centrality identify the most important vertices within a graph. Applications include identifying the most influential person(s) in a social network, key infrastructure nodes in the Internet or urban networks, and super-spreaders of disease. Centrality concepts were first … Continue reading A short summary of centrality in graph theory
How to rename all files in a folder using Python
let's assume the folder is user_data, relative to the current working directory (where the Python script is in). The code below was tested on Windows. import os for filename in os.listdir("./user_data"): # print(filename) cwd = os.path.dirname(os.path.abspath(__file__)) + '\\user_data\\' if filename.split('.')[1] == 'jpg': new_filename = filename.split('.')[0] + '_search' + '.jpg' print(new_filename) … Continue reading How to rename all files in a folder using Python
class << self in Ruby
First, the class << foo syntax opens up foo's singleton class (eigenclass). This allows you to customize the behavior of methods called on that specific object. a = 'foo' class << a def inspect '"bar"' end end a.inspect # => "bar" a = 'foo' # new object, new singleton class a.inspect # => "foo" ow, … Continue reading class << self in Ruby
define_method in Ruby
define_method is a (private) method of the object Class. You are calling it from an instance. define_method(:my_method) do |foo, bar| # or even |*args| # do something end or, you could even use string interpolation in the method name, such as define_method("#{attribute}=") do |unencrypted_password| if unencrypted_password.nil? self.send("#{attribute}_digest=", nil) elsif !unencrypted_password.empty? instance_variable_set("@#{attribute}", unencrypted_password) cost = ActiveModel::SecurePassword.min_cost … Continue reading define_method in Ruby
new update about has_secure_password in Rails 5.2
has_secure_password takes an attribute For many years has_secure_password only allowed a default password attribute. But now you can stash whatever you want in there. Source code Allows configurable attribute name for #has_secure_password. This still defaults to an attribute named 'password', causing no breaking change. Also includes a convenience method #<strong><span style="color:#008000;">regenerate_XXX</span></strong> where +XXX+ is the name … Continue reading new update about has_secure_password in Rails 5.2
How to Use link_to helper in Rails
Having documentation at hand Using documentation locally is helpful, especially if you can integrate it into your editor. Personally I’m using the ri docs with Emacs and Vim but I guess all those other editors can be configured to access it. If not you can always use ri in your terminal of choice. Another favourite … Continue reading How to Use link_to helper in Rails
Lambda functions in Python
A lambda function in python has the following syntax. Syntax of Lambda Function in python lambda arguments: expression Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required. Example: double = lambda x: x * 2 # Output: … Continue reading Lambda functions in Python
What is gemfile in a Ruby or Rails application
Ruby developers use Gemfiles all the time, and most of us know how to do the basics. In this post I want to dive deep into everything we can do with a Gemfile. What is a Gemfile? A Gemfile is a file we create which is used for describing gem dependencies for Ruby programs. A … Continue reading What is gemfile in a Ruby or Rails application
HTTP Request in Rails
Request is an object in ActionDispatch (ActionDispatch::Request < Object ), which in turn is part of ActionPack. It is available in Rails helper (which is available in View). Controller is inherited from ActionController. The request object is typically only accessible from the controller and the view. You could just pass the request to the methods … Continue reading HTTP Request in Rails