Category

Posts

List of notices what allow me to make my code faster. I save all best code practice what I found out or implemented before. If you find my notice helpful, please leave a comment.

is_a? and respond_to? Ruby methods

By Posts No Comments

is_a? Method that tells you whether an object has a given class either as its class or as one of its class’s ancestral classes:

>> mag = Magazine.new
=> #<Magazine:0x36289c>
>> mag.is_a?(Magazine)
=> true
>> mag.is_a?(Publication)
=> true

respond_to? – the method that lets you determine in advance whether an object knows how to handle a particular method – can come in handy.

Method notation

By Posts No Comments

Examples of this notation and what they refer to:

  • Ticket#price refers to the instance method price in the class Ticket.
  • Ticket.most_expensive refers to the class method most_expensive in the class Ticket. This notation is used when calling the most_expensive
    method.
  • Ticket::most_expensive also refers to the class method most_expensive in
    the class Ticket.

class Ticket
  def initialize(venue, date)
    @venue = venue
    @date = date
  end

  attr_reader :venue, :date
  attr_accessor :price
end

# *tickets Means any number argument
# The syntax &: is an abbreviated way of iterating over each of the elements in the tickets array and selecting the largest number. &: is often used with the map method, iterating over each element in an array, hash, or range and applying a method:
def Ticket.most_expensive(*tickets)
  p tickets.class
  tickets.max_by(&:price)
end

th = Ticket.new("Town Hall","2013-11-12")
cc = Ticket.new("Convention Center","2014-12-13")
fg = Ticket.new("Fairgrounds", "2015-10-11")
th.price = 12.55
cc.price = 10.00
fg.price = 18.00
highest = Ticket.most_expensive(th,cc,fg)
puts "The highest-priced ticket is the one for #{highest.venue}."

puts "Testing the response of a ticket instance...."
wrong = fg.most_expensive

.dub metod, which duplicates an object

By Posts No Comments
 
def 
 change_string(str)
 str.replace("New string content!") 
end
 
s = "Original string content!"
change_string(s.dup)

puts s 

Or you can use .freeze method

s = "Original string content!"
s.freeze

change_string(s)

Note that there’s no corresponding unfreeze method. Freezing is forever.

Also, you can use clone. It’s like as dup , but if you clone a frozen object, the clone is also frozen—whereas if you dup a frozen object, the duplicate isn’t frozen.


Commonly used Ruby command-line

By Ruby, Ruby extensions and programming libraries No Comments
Commonly used Ruby command-line switches
SwitchDescriptionExample of usage
-cChecks the syntax of a program file without executing the programruby -c c2f.rb
-wGives warning messages during program executionruby -w c2f.rb
-eExecutes the code provided in quotation marks on the command lineruby -e ‘puts “Code demo!”‘
-lLine mode: prints a newline after every line of outputruby -le ‘print “+ newline!”‘
-rnameRequires the named featureruby –rprofile
-vShows Ruby version information and executes the program in verbose moderuby –v
–versionShows Ruby version informationruby –-version
-hShows information about all command-line switches for the interpreterruby –h

Difference between require and load

By Posts, Ruby, Ruby extensions and programming libraries No Comments
require

is more abstract than load. Strictly speaking, you don’t require a file; you require a feature. And typically, you do so without even specifying the extension on the filename. To see how this works, change this line in test.rb

 load "test.rb"

to this:

 require "./test"

When you run test.rb, you get the same result as before, even though you haven’t supplied the full name of the file you want loaded.

Ruby. Configuration information

By Posts No Comments

Basic formula: RbConfig:: CONFIG[“term”].
Below outlines the terms and the directories they refer to.

 

TermDirectory contents
rubylibdirRuby standard library
bindirRuby command-line tools
archdirArchitecture-specific extensions and libraries (compiled, binary files)
sitedirYour own or third-party extensions and libraries (written in Ruby)
vendordirThird-party extensions and libraries (written in Ruby)
sitelibdirYour own Ruby language extensions (written in Ruby)
sitearchdirYour own Ruby language extensions (written in C)

 

Exaple

$ irb --simple-prompt -r rbconfig
>> RbConfig::CONFIG["bindir"]

 

Adde controllers concerns for devise params

By Devise, Ruby on Rails No Comments

Imagine that you decide to add a new user registration field. Let it be the name field After adding a field to the View You must add information about the new field to the controller. You can add it to the application_controller.rb file, but this is not the best solution. Imagine that your application will become very large. Imagine what your controller will look like. To get rid of this problem, I use modules

1 Step. Create module in foulder controllers/concerns. Create file, let’s say devise_whitelist.rb

module DeviseWhitelist
  extend ActiveSupport::Concern
    included do before_flter :configura_permitted_parameters, if: :devise_controller?
  end
  def configura_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:name])
  end
end

2 Step. Include module to main controller application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  
  include DeviseWhitelist
end