---
title: "常用小技巧"
author: "selfcontroller"
date: "2014-02-17T10:01:37Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2014-02-17T10:01:37Z"
id: 2017916
url: "https://selfcontroller.iteye.com/blog/2017916"
---

1 ruby  hash to array
  {a: 1, b: '2'}.map{|k,v| "#{k} = #{v}"}

  # => 

  arr = , , , ]

  Hash

  # =>  {"a"=>"1", "b"=>"2", "c"=>"3", "d"=>"4"}


2 ruby get array element
  arr = 
  a,b,c d = *arr

  a # => item 1
  b # => item 2
  c # => item 3
  d # => item 4


3 ruby array to  hash 
  a = 
  h = Hash

  # => {"item 1"=>"item 2", "item 3"=>"item 4"}



4 ruby two array element  adding  

  a = 

  b = 

  a.zip(b)

  # = > ,,,]

  a.zip(b).map{|x,y| x + y}

  # => 


5 ruby Regexp  return true or false

 /^\(\d{3}\)\s{1}\d{3}-\d{4}$/ === "(123) 456-7890"

 # => true


6 ruby string format

  '(%d%d%d) %d%d%d-%d%d%d%d' % 

  # => (123) 456-7890


7 ruby string to array

   str = "a=1, b=2, c=3, d=4, dddd"

   arr1 = str.split(', ')

   # => 


   arr2 = str.scan(/\w=\d/)

   # =>  

   
8 ruby string append

  str = ''

  str << 'a'  # => 'a'

  str << 'b'  # => 'ab'

  str << 'c'  # => 'c'

9 ruby string insert

  str = 'world'

  str.insert(0,'hello')

  # => 'hello world'