Array to Hash Ruby

That’s it. The * is called the splat operator. One caveat per @Mike Lewis (in the comments): “Be very careful with this. Ruby expands splats on the stack. If you do this with a large dataset, expect to blow out your stack.” So, for most general use cases this method is great, but use a … Read more

Rails button_to vs. HTML Button Tag

button_to like all others helpers for the views in Rails (e.g. link_to, form_for, image_tag…) simply converts your Rails command in HTML code. We could say that is a shortcut to write “pure HTML”. In your case, for button_to, you can see the reference (and what is printed) here: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to For example: I suggest you to … Read more

Difference between map and collect in Ruby?

There’s no difference, in fact map is implemented in C as rb_ary_collect and enum_collect (eg. there is a difference between map on an array and on any other enum, but no difference between map and collect). Why do both map and collect exist in Ruby? The map function has many naming conventions in different languages. Wikipedia provides an overview: The map function originated in functional programming languages but is today supported (or may be … Read more

What does the Ruby method ‘to_sym’ do?

to_sym converts a string to a symbol. For example, “a”.to_sym becomes :a. It’s not specific to Rails; vanilla Ruby has it as well. It looks like in some versions of Ruby, a symbol could be converted to and from a Fixnum as well. But irb from Ruby 1.9.2-p0, from ruby-lang.org, doesn’t allow that unless you add your own to_sym method to Fixnum. I’m … Read more