Ruby on Rails Friday, February 6, 2015



On Friday, February 6, 2015 at 12:21:09 PM UTC, Ruby-Forum.com User wrote:
can u help me understand this code
<a class='filter'
href='/blog?month=" + monthname[0..-6] + "&year=" + monthname[3..8] +
"'>" + date_value + "</a>


the numbers and all

This is the String#[] method http://www.ruby-doc.org/core-2.2.0/String.html#method-i-5B-5D 

monthname[0..-6] returns the string from the first character until the one 6 from the end (negative indexes count from the end of strings)

monthname[3..8] returns from index 3 (i.e. the 4th character) until the one at index 8 

So if your string is "06 2015" then monthname[0..-6] is "06" and monthname[3..8] returns "2015"  (given the length of the string monthname[3..6] would return the same string)

Using strings that have to be torn apart like this does seem a little bit obtuse though.

Fred


--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/80bd132e-5d1c-4cca-b2cc-54f112e5232f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment