SIDEBAR
»
S
I
D
E
B
A
R
«
LOGO PEPSI & COKE
Sep 20th, 2009 by batt

Ruby Percent Syntax (Percent Functions)
Sep 8th, 2009 by batt

I wanted to post a quick guide to the special ruby syntax for literals that utilize the % (percent) symbol. Most beginners guides to ruby leave out an explanation of these forms of literals, but many ruby coders use them. When someone encounters them for the first time it is almost impossible to figure out what they mean. (Try searching Google for ?%w?)

Ruby has special syntax for making strings, arrays and system commands easier to write. They allow you to use different characters as delimiters so you can minimize escaping in your literals.

The syntax

The syntax for the % literals is a percent symbol (%) a letter which defines what kind of literal it is (Q, q, w, x, r) a delimiter,? the content, and the closing delimiter.

The delimiter can be any character, and is defined as whatever is immediately after the letter in the syntax. For example %Q!content! , the delimiter is the ! and it surrounds the content. There are special cases when the delimiter is { or (, the closing delimiter will be } or ) respectively.

%Q and %q (Percent Q): Strings

%Q!Some String of ?Characters?! <==> ? Some String of \?Characters\? ?

%Q is the equivalent to a double-quoted ruby string. #{expression} evaluation works just like in double-quoted strings, even if you use %Q{} as your delimiter!

You can also leave off the Q and it will have the same functionality. I recommend leaving the Q in to be more clear.

%q!Some String of ?Characters?! <==> ?Some String of Characters?

The %q is just like %Q, but acts the same as a single-quoted string. Whatever is inside the delimiters is returned as a string.

You can remember %Q is for strings because it acts like Quotes.

More info here: http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#string

%W (Percent W): Arrays

%W(North South East West) <==> [“North”, “South”, “East”, “West”]

%W (and %w) allow you to create an Array of strings without using quotes and commas.

The delimiter rules are the same as strings, but typically parentheses are used. The content inside the delimiters are split by white-space, and put into an array. This is great if you have a hard coded list of single word strings.

When using %W (capital W), it is evaluated as a double-quoted string. This allows you to use #{} to interpolate values. %w (lower-case w) will evaluate as a single quoted string.

You can remember %W is by thinking of it as a White-space divided Array.

More info here : http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#array

%x (Percent x): System Execution

%x{ ls /usr/local } <==> `ls /usr/local`

%x allows you to call system commands, equivilent to wrapping the command in `s (grave accents). The benefit of the $x{} syntax is you don?t have to escape your accents in commands that use them.

You can remember to use X because it eXecutes a command.

More info here: http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#command

%r (Percent r): Regular Expressions

%r{/usr/bin/} <==> /\/usr\/bin\//

%r is really handy for regular expressions that contain /s (forward slashes) which are the default delimiter for regular expressions and have to be escaped.

Remember to use %r with regular expressions.

More info here: http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#regexp

I hope this information is helpful. Please leave a comment if this helped or if I left something out.

Tags: , , ,

credit : http://jimhoskins.com/2008/10/07/ruby-percent-syntax-percent-functions/

50 โรคของหัวหน้า
Sep 7th, 2009 by batt

หัวหน้าของใครอันไหนไปเลือกเอาเองนะ!!!

50 โรคของหัวหน้า (ใครไม่รุ) :-
โรคที่ 1 : โรคทำงานไร้เป้าหมาย
โรคที่ 2 : โรคสั่งงานแบบไร้สติ
โรคที่ 3 : โรคทองไม่รู้ร้อน
โรคที่ 4 : โรคชอบให้ทุกคนมีนิสัย (ประหลาด) เหมือนตัวเอง
โรคที่ 5 : โรคชอบพูดว่า ประตูห้องของผมเปิดกว้างรับฟังความคิดเห็นเสมอ (แต่.. ผมไม่เคยเปิดประตูเลย)
โรคที่ 6 : โรคชีวิตนี้ (ตู) มีแต่งาน งาน งาน
โรคที่ 7 : โรคเพื่อนเราชอบเผาเรือน
โรคที่ 8 : โรคชอบใช้ศัพท์ภาษาอังกฤษหรูๆ
โรคที่ 9 : โรคทำตัวไร้ตัวตน
โรคที่ 10 : โรคผู้บังคับบัญชานาซี
โรคที่ 11 : โรคไม่มีปัญหาครับท่าน
โรคที่ 12 : โรคเสมือนจริงใจ
โรคที่ 13 : โรครู้ไปหมดซะทุกเรื่อง
โรคที่ 14 : โรคทำงาน (ลึก) ลับอะไรไม่รู้
โรคที่ 15 : โรค “เลื่อนขั้นเหรอ” ชาติหน้าบ่ายๆ
โรคที่ 16 : โรคตัดสินใจ ทำไมยากจัง!
โรคที่ 17 : โรคแค้นฝังหุ่น (จำไม่ได้..แมร่งใช้จดเลยอะ)
โรคที่ 18 : โรคหวงก้างไว้ทำเกลือหรือไง?
โรคที่ 19 : โรคคลั่งไคล้การประชุม (บ้าหรือปล่าว?)
โรคที่ 20 : โรคมีลูกน้องเหมือนจะฉลาด
โรคที่ 21 : โรคชอบชมลูกน้องจัง! (แต่ลับหลังคนอื่นฮ่ะ!)
โรคที่ 22 : โรคดีเป็นของตัว ชั่วเป็นของคนอื่น (ลูกน้อง!)
โรคที่ 23 : โรคชอบแทงข้างหลังคนอื่น (น่ากลัวจังเลยฮ่ะ!)
โรคที่ 24 : โรคจุ้นจ้านไปหมดทุกเรื่อง (คุณคิดว่าคุณเป็นใครหรอ?)
โรคที่ 25 : โรคชอบเล่าเรื่องโจ๊ก (แต่มันไม่ค่อยตลกเลย..เจ้านาย!)
โรคที่ 26 : โรคเดี่ยวไมโครโฟน (บ้าน้ำลายจริงๆ จ๊ะ!)
โรคที่ 27 : โรคฉันชอบประชุมตอนเช้า ใครจะทำไม? (ก็ตรูว่างคนเดียวอะ..คนอื่นชั่งมัน)
โรคที่ 28 : โรคไม่ชอบประชุมเอาซะเลยกับเจ้านาย)
โรคที่ 29 : โรคเก่งคนเดียว คิดคนเดียว (แต่ตอนลงมือทำ.พี่ฝากด้วยนะ เก็บโน็ตบุคเดินกลับบ้าน)
โรคที่ 30 : โรคลูกน้องทะเลาะกันเหรอ? ฉันไม่สนใจหรอกย่ะ
โรคที่ 31 : โรคมีลูกน้องที่คิดว่า ตัวเองเป็น Superman
โรคที่ 32 : โรคข้าพเจ้าถูกแต่เพียงผู้เดียว (เก่งไม่ทำเองอะ)
โรคที่ 33 : โรคชอบมีไอเดียใหม่ หลังลูกน้องทำงานเสร็จ
โรคที่ 34 : โรคนั่งอ่านเมล์ยังไม่ทันจบแล้ว FW. ให้เราไปทำ (อ้าวไม่ใช่งานตรู…..นิหว่า)
โรคที่ 35 : โรคลดขนาดองค์การเหรอ? อืม ตัวใครตัวมัน
โรคที่ 36 : โรคพนักงานชั่วคราวเหรอ เมินซะเถอะ!
โรคที่ 37 : โรคไม่เคยติดตามการทำงานของลูกน้อง
โรคที่ 38 : โรคเตรียมพร้อมแปลว่าอะไร (ไม่รู้จักจริงๆ น่ะ!)
โรคที่ 39 : โรคจมกองเอกสารแต่งานไม่คืบหน้าไปไหน
โรคที่ 40 : โรคหวงโอที ( ตัวเองแอบมาทำ..อิ..อิ อันนี้เรื่องจริง)
โรคที่ 41 : โรค “คุณภาพ” เหรอ? อืม! แปลว่าอะไรล่ะ….(อ๋อ รีบๆทำพรุ่งนี้จะ Present เจ้านาย)
โรคที่ 42 : โรคจำลูกน้องคนโปรดได้คนเดียว!
โรคที่ 43 : โรคประเมินผลงานด้วยวิธีที่ฉลาดน้อยที่สุด…(พิจารณาจากพรรคพวกตัวเองเป็น หลัก)
โรคที่ 44 : โรคชอบสะสม “ระเบิดเวลา”..( รับมาวันอังคาร..ดองไว้สั่งเราวันศุกร์เย็นบอกขอวันจันทร์เช้า..บ้าป่าววะ)
โรคที่ 45 : โรคนั่งจับผิดลูกน้อง ( เมื่อวานเข้าห้องน้ำ 3 ครั้ง นั่งหาว 2 ครั้ง ตดครั้งนึง เป็นต้น)
โรคที่ 46 : โรคปฏิเสธใครไม่เป็น (รับทุกเรื่อง…แล้วมาโยนให้ตรูทำ)
โรคที่ 47 : โรคปกครองระบบทาส
โรคที่ 48 : โรคทำตัวเครียดทั้งวัน (นั่งถอนหายใจ..เฮ้อ)
โรคที่ 49 : โรคมนุษย์ผู้สมบูรณ์แบบ (ตลอดกาล!)
โรคที่ 50 : โรคมนุษย์เจ้าปัญหา (ทุกเรื่อง )

คลายเครียดอย่าไปคิดมาก…..มีงานทำก็ดีแล้ว

SIDEBAR
»
S
I
D
E
B
A
R
«
»  Substance:WordPress   »  Style:Ahren Ahimsa