Skip to content

Instantly share code, notes, and snippets.

View pvin's full-sized avatar

praaveen vr pvin

View GitHub Profile
@pvin
pvin / lift_to.rb
Created July 6, 2014 07:28
ruby code for lift(elevator)
class Lift
GROUND_FLOOR = 1
TOTAL_FLOOR = 10
@@previous_floor =0
TRAVEL_TIME = 3
DO_DC = 10
attr_reader :elevator_on_floor
attr_accessor :elevator_target_floor, :new_floor
@pvin
pvin / hello.exs
Created November 21, 2015 09:28
hello world in elixir
IO.puts "hello world!"
events/_forms.html.erb
<div class="form-group test">
<%= f.label :auctions, class: "col-lg-4 col-sm-2 control-label required" %>
<div class="col-lg-8" id="filter">
<%= f.collection_select :auction_ids, filter_auction, :id, :name, {}, {:multiple => true, class: "form-control acution-class", required: true}%>
</div>
</div>
<script>
@pvin
pvin / rb
Last active May 6, 2019 01:46
inject method in ruby(reference : Eloquent ruby)
class Document
def average_word_length
total = 0.0
words.each { |word| total += word.size }
total / word_count
end
end
#This is the kind of problem that the inject method is tailor-made to solve. Like each , inject takes a block and calls the
#block with each element of the collection. Unlike each , inject passes in two arguments to the block: Along with each
@pvin
pvin / rb
Created May 29, 2016 10:06
Carefull with processing array index, ruby. (reference : Eloquent ruby)
#The easiest way to screw up one of these iterating methods is to change the collection out from underneath the method.
#Here, for example, is a seriously misguided attempt to remove all of the negative numbers from an array:
array = [ 0, -10, -9, 5, 9 ]
array.each_index {|i| array.delete_at(i) if array[i] < 0}
pp array
#The trouble with this code is that it will tend to leave some negative numbers behind: Removing the first one (the -10 ) from
#the array messes up the internal indexing of the each method, so much that it will miss the second negative number, leaving
#us with a result of:
#Usual way
unique = []
words.each { |word| unique << word unless unique.include?(word) }
#What we really need is a collection that doesn’t allow duplicates but does feature very fast and easy answers to the “is this
#object in there?” question. The punch line is that we need a set. Fortunately, Ruby comes with a perfectly serviceable, if
#sometimes forgotten,
#Set class:
@pvin
pvin / gist:f7c8c41fd67116bc92ec011ddfee6558
Created June 6, 2016 12:26
ruby - difference between chop and chomp
#Be careful not to confuse chomp with the chop method. The chop method will simply knock off the last character of the
#string, no matter what it is
"hello\n".chomp
#=> "hello"
"hello".chomp
#=> "hello"
"hello".chop
puts 'yes yes'.sub( 'yes', 'no' )
=> no yes
puts 'yes yes'.gsub( 'yes', 'no' )
=> no no
Rspec::
rspec
rspec spec/models
rspec spec/controllers/accounts_controller_spec.rb
rails generate rspec:model
/factories /support /models /controllers /features /views /mailers /routing /helpers
Model spec - behavior of model
require "rails_helper"
@pvin
pvin / psql_reference.txt
Last active January 31, 2018 11:30
psql
login with peer authondication
sudo -u postgres psql
login with md5
Then type:
sudo -u postgres psql
Then: