Use Redis with Ruby (learn fast, forget faster)

First thing first: Install redis gem and then require it from you ruby code:

 require 'redis'

Example 1: Create a new Redis client instance

 r = Redis.new

Example 2: List all keys

 r.keys

Example 3: Append a value to a list, show ALL the values of the list, pop one value from the left of the list

 r.RPUSH("mylist", "myvalue")
 r.LRANGE("mylist", 0, -1)
 r.LPOP("mylist")

Example 4: Insert a value from the left of the list, read the value without removing it from the list

 r.LPUSH("mylist", "myvalue")
 r.LINDEX("mylist", 0)

Use ruby gem “mechanize” as httpclient

Need to require ‘mechanize’

Simple login example:
method 1:


    login_form = @agent.get("#{DD_DEV_ADMIN_LOGIN_URL}").forms[0]
    user_email_input = login_form.field_with(:name => 'admin_user[email]')
    user_password_input = login_form.field_with(:name => 'admin_user[password]')
    user_email_input.value = USERNAME
    user_password_input.value = PASSWORD
    @agent.submit(login_form, login_form.buttons.first)

method 2:

Ruby String.scan and sprintf example

scan(/\w+/): covert ruby string to string array.
sprintf ‘%02d’: in this example it converts 1 to 01

 chapter_node_index = "Chaper 1 Node 1".scan(/\w+/)
 chapter_index = sprintf '%02d', chapter_node_index[1]
 node_index = sprintf '%02d', Integer(chapter_node_index[3])-1
 p ("Chapter_#{chapter_index}_Buildings_#{node_index}")