Skip to content

Instantly share code, notes, and snippets.

@suryart
suryart / removing last word from a sentence (a string)
Created October 3, 2013 13:35
How to go about removing the last element? For example, I have strings like these: str1 = "My testing String" str2 = "My another testing string" I need a neat way of showing the output: str1 = "My testing" str2 = "My another testing" This is what I could do: str1 = str1.split(" ") str1.delete(str1.last) str1.join(" ") # => "My testing" I was won…
#!/usr/bin/ruby
require 'benchmark'
str2 = "My another testing well string"
n = 500
Benchmark.bm(40) do |x|
x.report("str2[/(.*)\s/,1] "){ n.times { str2[/(.*)\s/,1] } }
x.report("str2[0...str2.rindex(' ')] "){ n.times { str2[0...str2.rindex(' ')] } }
x.report("str2.split(' ')[0...-1].join(' ') "){ n.times { str2.split(' ')[0...-1].join(' ') } }
x.report("str2[/.*(?=\s)/] "){ n.times { str2[/.*(?=\s)/] } }
end
@suryart
suryart / fizzbuzz.rb
Last active August 24, 2016 19:32
Write a program which prints the numbers from 1 to N, each on a new line. But for multiples of three print “Fizz” instead of the number 3 and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. Read in the input number from STDIN.Solution below is in ruby.
1.upto(STDIN.gets.to_i) do |num|
break if num > 10**7 # num should be less than 10^7
output = nil
output = output.to_s + 'Fizz' if num % 3 == 0
output = output.to_s + 'Buzz' if num % 5 == 0
puts output || num
end
@suryart
suryart / graph.rb
Created September 4, 2013 15:59
Locating the route of the shortest path of a map(connected nodes with weight/distance) in Ruby
# !/usr/bin/ruby1.9.2
# encoding: utf-8
class Node
attr_accessor :src, :dst, :length
def initialize(src, dst, length = 1)
@src = src
@dst = dst
@length = length
end
@suryart
suryart / spec_helper.rb
Created July 4, 2013 08:18
spec_helper for spree testing. Enables :js to run without any warning.
# Configure Rails Environment
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../dummy/config/environment.rb', __FILE__)
require 'rspec/rails'
require 'database_cleaner'
require 'ffaker'
require 'factory_girl'
@suryart
suryart / output
Created July 3, 2013 18:20
console output for order spec example for spree frontend
[#<Spree::Product id: 1, name: "Product #1 - 6731", description: "Dicta tenetur maxime velit facere. In quisquam odit...", available_on: "2013-07-02 18:13:39", deleted_at: nil, permalink: "product-number-1-6731", meta_description: nil, meta_keywords: nil, tax_category_id: 1, shipping_category_id: 1, created_at: "2013-07-03 18:13:40", updated_at: "2013-07-03 18:13:40">]
#<Spree::Product id: 1, name: "Product #1 - 6731", description: "Dicta tenetur maxime velit facere. In quisquam odit...", available_on: "2013-07-02 18:13:39", deleted_at: nil, permalink: "product-number-1-6731", meta_description: nil, meta_keywords: nil, tax_category_id: 1, shipping_category_id: 1, created_at: "2013-07-03 18:13:40", updated_at: "2013-07-03 18:13:40">
/products/product-number-1-6731
F
Failures:
1) Orders should allow a user to see product name
Failure/Error: find('.product-title').text.should equal product.name
Capybara::ElementNotFound:
@suryart
suryart / order_spec.rb
Last active December 19, 2015 07:49
orders spec example for spree frontend
require 'spec_helper'
describe 'Orders' do
let!(:product) { create(:product, :available_on => 1.day.ago) }
stub_authorization!
before(:each) do
visit spree.admin_path
click_link "Products"
source 'https://rubygems.org'
gem 'rails', '3.2.13'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
➜ my_store rvm:(ruby-2.0.0) bundle update
Fetching https://github.com/spree/spree.git
remote: Counting objects: 130666, done.
remote: Compressing objects: 100% (48435/48435), done.
remote: Total 130666 (delta 82214), reused 119957 (delta 72967)
Receiving objects: 100% (130666/130666), 27.04 MiB | 380.00 KiB/s, done.
Resolving deltas: 100% (82214/82214), done.
Updating https://github.com/spree/spree_gateway.git
Updating https://github.com/spree/spree_auth_devise.git
Updating git://github.com/suryart/spree_active_sale.git
activerecord (3.2.13) lib/active_record/persistence.rb:347:in `create_or_update'
activerecord (3.2.13) lib/active_record/callbacks.rb:264:in `block in create_or_update'
activesupport (3.2.13) lib/active_support/callbacks.rb:425:in `_run__3652904023592825844__save__1349650235203356801__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.13) lib/active_record/callbacks.rb:264:in `create_or_update'
activerecord (3.2.13) lib/active_record/persistence.rb:84:in `save'
activerecord (3.2.13) lib/active_record/validations.rb:50:in `save'
activerecord (3.2.13) lib/active_record/attribute_methods/dirty.rb:22:in `save'
@suryart
suryart / statement.rb
Created October 29, 2012 18:42
Travel Allowance Statement generation Script
t1 = (Time.now-6.months).beginning_of_month # Start Date Time
t2 = (Time.now).end_of_month # End Date and Time
t = t1
amounts = [155, 140, 160]
from = "Banashankari 3rd Satge, Vyalikaval"
to = "Vyalikaval, Banashankari 3rd Satge"
month = t.month
year = t.year
leaves = {"26/01/2012" => "Republic Day", "23/03/2012" => "Chandramana Ugadi", "01/05/2012" => "May Day", "15/08/2012" => "Independence Day", "19/09/2012" => "Ganesha Chaturthi", "02/10/2012" => "Gandhi Jayanti", "23/10/2012" => "Audha Pooja", "01/11/2012" => "Kannada Rajyotsava", "12/11/2012" => "Naraka Chaturdashi", "14/11/2012" => "Balipadyami" }