Notes posted to Ruby
RSS feedOUTDATED!!!
Man, this stuff is so outdated. Be very careful using anything from here. A lot has changed since Ruby 1.9.
You’ll want to look at the updated docs, like here for Ruby 2.5.1:
ruby-doc.org/core-2.5.1/Time.html#method-i-strftime
They really should just take this site down if they’re not going to keep it updated. Probably does more harm than good now.
define_method with blocks works differently
As it is already stated that block is evaluated using instance_exec/instance_eval, so let me give you an example.
module Service module ClassMethods def endpoint_instance_exec(name, &block) define_method name do instance_exec(&block) end end def endpoint_block_call(name, &block) define_method name, &block end def endpoint_block_improper_call(name, &block) define_method name do # In this case, we called the block without "instance_eval" that # means block was called in the context of class MyService. block.call end end end def self.included(klass) klass.extend ClassMethods end private def hello puts 'world' end end class MyService include Service endpoint_instance_exec :foo do hello end endpoint_block_call :bar do hello end endpoint_block_improper_call :foobar do hello end end
Now, understand how can we execute the code and understand the working of define_method and instance_exec.
MyService.new.foo # => "hello" MyService.new.bar # => "hello" MyService.new.foobar # => undefined local variable or method `hello' for MyService:Class
Urlify Functions & Its Implementation
URLify is a simple gem that refines the conversion of UTF-8 strings to ASCII-safe URI strings and enables it to be used as readable URL-segments. After the gem is installed, you can call the URLify function for any UTF-8 string and it will be automatically converted into an ASCII-safe URI string. URLify also has the additional functionality of being able to remove the subtitles in a given input. ACCENTMAP
‘À’ => ‘A’, ‘Á’ => ‘A’, ‘Â’ => ‘A’, ‘Ã’ => ‘A’, ‘Ä’ => ‘A’, ‘Å’ => ‘AA’, ‘Æ’ => ‘AE’, ‘Ç’ => ‘C’, ‘È’ => ‘E’, ‘É’ => ‘E’, ‘Ê’ => ‘E’, ‘Ë’ => ‘E’, ‘Ì’ => ‘I’, ‘Í’ => ‘I’, ‘Î’ => ‘I’, ‘Ï’ => ‘I’, ‘Ð’ => ‘D’, ‘Ł’ => ‘L’, ‘Ñ’ => ‘N’, ‘Ò’ => ‘O’, ‘Ó’ => ‘O’, ‘Ô’ => ‘O’, ‘Õ’ => ‘O’, ‘Ö’ => ‘O’, ‘Ø’ => ‘OE’, ‘Ù’ => ‘U’, ‘Ú’ => ‘U’, ‘Ü’ => ‘U’, ‘Û’ => ‘U’, ‘Ý’ => ‘Y’, ‘Þ’ => ‘Th’, ‘ß’ => ‘ss’, ‘à’ => ‘a’, ‘á’ => ‘a’, ‘â’ => ‘a’, ‘ã’ => ‘a’, ‘ä’ => ‘a’, ‘å’ => ‘aa’, ‘æ’ => ‘ae’, ‘ç’ => ‘c’, ‘è’ => ‘e’, ‘é’ => ‘e’, ‘ê’ => ‘e’, ‘ë’ => ‘e’, ‘ì’ => ‘i’, ‘í’ => ‘i’, ‘î’ => ‘i’, ‘ï’ => ‘i’, ‘ð’ => ‘d’, ‘ł’ => ‘l’, ‘ñ’ => ‘n’, ‘ń’ => ‘n’, ‘ò’ => ‘o’, ‘ó’ => ‘o’, ‘ô’ => ‘o’, ‘õ’ => ‘o’, ‘ō’ => ‘o’, ‘ö’ => ‘o’, ‘ø’ => ‘oe’, ‘ś’ => ‘s’, ‘ù’ => ‘u’, ‘ú’ => ‘u’, ‘û’ => ‘u’, ‘ū’ => ‘u’, ‘ü’ => ‘u’, ‘ý’ => ‘y’, ‘þ’ => ‘th’, ‘ÿ’ => ‘y’, ‘ż’ => ‘z’, ‘Œ’ => ‘OE’, ‘œ’ => ‘oe’, ‘&’ => ‘and’
Easy Steps To Implement URLify Gem
Go to the Gemfile and add the gem urlify Run the command bundle install
OR In the terminal, run the command gem install urlify A Demo Of Implementation Of URLify
Here is an example of URLify functionality:
Add gem urlify in your Gemfile Run bundle install
Read More From Here http://www.railscarma.com/blog/technical-articles/urlify-functions-implementation/
Faker Gem: Fake Data Generation in Ruby
Gems are libraries in Rails that generally enable you to write the application code faster and thereby making a great product in far lesser time. Usually, whenever we start developing any application, there comes a point when we need data which we can use to see how the application will behave while doing some load testing or how it would look when we deploy it to the production. The manual process of creating the data can be daunting. Faker gem serves to take this pain away by generating the fake data just as needed and saving us all the time and effort otherwise wasted in the manual process of data-generation.
It can generate almost any kind of data suitable for our application. For example, it can generate the fake data for fields such as name, email, passwords, phone-numbers, paragraphs, etc. It is therefore, an awesome way of populating the model (which is a database layer in Rails)
Let’s take a look at this gem by creating a sample project. Read More: http://www.railscarma.com/blog/technical-articles/faker-gem-fake-data-generation-ruby/
And yet another way to get relative path from absolute globbing
If you execute glob within a block passed to Dir.chdir, you get the paths relative to the directory specified by Dir.chdir… like this…
base_dir = '/path/to/dir' files = Dir.chdir(base_dir) do Dir.glob("**/*.yml") end files.first # => 'foo/bar.yml'
nope
If you pass nil as local file, it doesn’t write the file and only returns the content as string.
Sort Integers
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]
Sort From Greatest to Smallest
>> [1, 2, 3, 4].sort { |a, z| z <=> a } => [4, 3, 2, 1]
Clarifying the confusing example
since exit is a keyword in Ruby, the example may be confusing. The following example might be less so:
module Foo begin # this raises an error b/c baz is not defined here
