Change a string into class name
In Ruby, class names are constants, so you can do
str = "Thing" #=> "Thing"
Kernel.const_get(str) #=> Thing
Related cards:
Abstract classes in rails
class AbstractModel < ActiveRecord::Base
self.abstract_class = true
end
class Foo < AbstractModel
end
class Bar < AbstractModel
end
Foo and Bar both inherit properties and methods of the Abstract Model while being in separate tables...
difference between attr_accessor and initialize
attr_accessor is used to create setter and getter methods for instance variables so that they can be accessed outside the class. initialize is the special method in ruby that gets called when an object is instantiated.
class Birthday
attr_ac...
listing methods of class and object (instance)
class Myclass
def one
puts "one is called"
end
end
ss = Myclass.new
puts Myclass.methods
# returns array of methods for the class. Includes both class and instance methods and methods inherited from parents.
puts Myclass.instanc...
Class_eval and Instance_eval
class_eval is called on a class object and is used to create instance methods for the class at runtime.
%w(administrator developer teamlead tester).each do |user|
class_eval <<-EOR
def is_#{user}?
site == "MYACCOUNT_#{org.upcase}" ...
Create class names from strings
"foo_and_bars".classify # => "FooAndBar" #String
"foo_and_bars".classify.constantize # => "FooAndBar" #Class
# in case of singular names
"business".classify # => "Busines"
"business".cameliz...
add sublime text option to the contextual menu
After installing sublime text, we must be able to open text files with it. Right click on a text file and you must see the option "open with sublime text". In case you don't , click Open with other applications and you should see Sublime text opti...
Fixing "uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)"
If you have to work with older versions of Rails, there might be discrepancy between the versions of Rails and Rubygems. Theats when you likely see the below message when you try to boot your rails app.
/home/lt-pc-528/.rvm/gems/ruby-1.8.7-p...
Adobe flash player installation in ubuntu chromium browser
For Chromium browser versions greater than 34, to use flash one must install Pepper flash.
For Ubuntu 14.04 or newer, run
sudo apt-get install pepperflashplugin-nonfree
sudo update-pepperflashplugin-nonfree --status
# to see what version of...
change string encoding in ruby
# Ruby 1.9
string_in_utf8_encoding = string_in_latin1_encoding.encode('UTF-8')
#Ruby 1.8
gem install iconv
require 'iconv'
string_in_utf8_encoding = Iconv.conv("UTF8", "LATIN1", string_in_latin1_encoding)
However, if you want to let Rub...
Passing objects using redirect_to
Scenario : You have some manipulation done in your controller action and redirect the user to another action after that. Now you want the output of the manipulation available in the page user is redirected to.
You can pass the object with redirec...