Rails 3 assumes that everything is NOT html safe (a change of opinion from Rails 2). Now, all strings are html escaped by default: <%= h some_string %> is now the same as <%= some_string %> To unescape the HTML (i.e you already know that the string is OK to render out), you need to mark it as html_safe or use keyword raw : <%= some_string.html_safe %> or <%=raw some_string...
In this post I present 21 different Ruby “tricks,” from those that most experienced developers use every day to the more obscure. Whatever your level, a refresh may help you the next time you encounter certain coding scenarios. Note to beginners: If you’re still learning Ruby, check out Beginning Ruby book. 1 – Extract regular expression matches quickly A typical way to extract data from text using a regular expression is to use the match method. There is a shortcut, however, that can take the pain out of the process: email = "Fred Bloggs <fred@bloggs.com>" email.match(/<(.*?)>/)[1] # => "fred@bloggs.com" email[/<(.*?)>/, 1] # => "fred@bloggs.com" email.match(/(x)/)[1] # => NoMethodError [:(] email[/(x)/, 1] # => nil email[/([bcd]).*?([fgh])/, 2] # => "g" Ultimately, using the String#[] approach is cleaner though it might seem more “magic” to you. It’s also possible to use it without including an extra argument if you just want to match the entire regular expression. For example: x = 'this is a test' x[/[aeiou].+?[aeiou]/] # => 'is i' In this example, we match the first example of “a vowel, some other characters, then another vowel.” 2 – Shortcut for Array#join It’s common knowledge that Array#*, when supplied with a number, multiplies the size of the array by using duplicate elements: [1, 2, 3] * 3 == [1, 2, 3, 1, 2, 3, 1, 2, 3] It’s not well known, however, that when given a string as an argument Array#* does a join! %w{this is a test} * ", " # => "this, is, a, test" h = { :name => "Fred", :age => 77 } h.map { |i| i...
I’m a programmer, among other things. Like many in this profession. I suck at designing UIs (though sometimes I believe I don’t). When you let programming-focused people like me build your user interface, you will get things like this: Joseph Cooney calls this The Dialog: A developer needed a screen for something, one or two text boxes and not much more, so they created “the dialog”, maybe just to “try something out” and always with the intention of removing it before the product ships. They discovered they needed a few more parameters, so a couple more controls were added in a fairly haphazard fassion. “The dialog” exposes “the feature”, something cool or quite useful. Admittedly “the feature” is more tailored towards power users, but it’s still pretty cool. The developer thinks of new parameters that would make “the feature” even more powerful and so adds them to the dialog. Maybe a few other developers or power users see “the dialog” and also like “the feature”. But why doesn’t it expose this parameter? New controls are added. Pretty soon the technical team are so used to seeing “the dialog” the way it is that they become blind to its strange appearance. Ship time approaches and the product goes through more thorough testing, and “the dialog” is discovered, but it is too late to be heavily re-worked. Instead it is given a cursory spruce-up. Some people are naturally gifted at user interface design. They feel physically sick about adding a button that clutters the interface or messes up the user’s workflow. Make sure you have a gifted UI designer on your...
web application singapore,singapore web design services,ruby on rails developer singapore,developers in singapore,singapore web design,web development company singapore,app developer singapore,web designer singapore,android developer singapore,mobile apps singapore,mobile application developer singapore,ios developer singapore,website design singapore,mobile app development singapore,mobile game developer singapore,singapore mobile application developer,web design services singapore,developer in singapore,website developer singapore,singapore web development,web design company singapore,web development singapore,website development singapore,singapore website design,app development singapore,website designer singapore,ios app development singapore,web design singapore,graphic designer in singapore,mobile application development singapore,singapore app developer,design firms in singapore,mobile apps development singapore,design agency singapore,singapore mobile app developer,mobile developer singapore,mobile app developer singapore