def locale_links
  [:pl, :en].map { |locale| link_to locale.to_s.upcase, {:locale => locale}, (:class => :language_selected if I18n.locale == locale)}.join ' | '
end

Ruby On Language Selection

by Martin Plöger, August 27, 2010 14:35 Star_fullStar_fullStar_fullStar_fullStar_full

I'd also put it in a helper...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
array.inject({}) { |hash, key| hash.merge! key => func(key) }

Ruby On How to make it more idiomatic?

by Martin Plöger, August 16, 2010 17:39

merge! is more efficient th...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
sum = items.inject { |memo, item| memo + item.fragments.count }

Ruby On Calculate sum of items in a...

by Martin Plöger, August 16, 2010 17:30 Star_fullStar_fullStar_fullStar_fullStar_full

You don't need to pass the ...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
def is_pos_activation?
  return yield if block_given? && session[:pos_activation]
  session[:pos_activation]
...

Ruby On better way?

by Martin Plöger, July 22, 2010 07:32 Star_fullStar_fullStar_fullStar_fullStar_full

@Adam: you call the block e...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
def check_win_streak streak
  return if streak % 10 != 0 || streak <= 0
  award_streak_badge [10, 20, 30][streak / 10 % 3 - 1]
...

Ruby On how to refactor tricky logi...

by Martin Plöger, June 08, 2010 16:55 Star_fullStar_fullStar_fullStar_fullStar_full

Hope, I understood what you...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
{ :param_one => 'foo', :param_two => 'bar' }.to_param

Ruby On Generate post query for array

by Martin Plöger, June 01, 2010 14:58 Star_fullStar_fullStar_fullStar_fullStar_full

Rails has a Hash#to_param-m...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
text =
"75
95 64
...

Ruby On I'm having trouble thinking...

by Martin Plöger, May 27, 2010 18:05
E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
b = {:a => 'option1', :b => 'option2' }
b.map { |key, value| "#{key}=#{value}" }.join '&'

Ruby On HTML query string

by Martin Plöger, April 29, 2010 04:44

Hash#map or Hash#collect wo...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
require 'rubygems'
require 'hpricot'
require 'open-uri'
...

Ruby On Can I do this while loop in...

by Martin Plöger, April 26, 2010 06:50 Star_fullStar_fullStar_fullStar_fullStar_full

I'm not quite satisfied wit...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
mappings = ['body', 1], ['height', 2], ['status', 7], ['country', 8, 1], ['zip', 9], ['location', 7]
mappings.each do |key, content_index, regexp_index| 
  data[key] = content[content_index].split(/\W{2,}/)[regexp_index || 0].strip rescue 'null'
...

Ruby On Exception Handling in Ruby

by Martin Plöger, March 21, 2010 21:43 Star_fullStar_fullStar_fullStar_fullStar_full

I just recycled Yury's grea...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
attributes.map { |key, value| "#{$1}.#{value}" if key =~ /^svc_(\w{2})_id$/ }.compact

Ruby On Ruby Hash Extraction Using ...

by Martin Plöger, March 08, 2010 20:53

@mxcl: Seems to be the most...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
File.open 'binary.bin', 'wb' do |file|
  b = BinData
  head = b::String, 'Binary file start-point', b::Uint32be, 0, b::Bit16,0, b::Uint24be, @fragments.size
...

Ruby On Creating a binary file with...

by Martin Plöger, March 05, 2010 20:51 Star_fullStar_fullStar_fullStar_fullStar_full

I don't know much about bin...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk

Ruby On Nested Hash from delimiter-...

by Martin Plöger, March 05, 2010 14:14 Star_fullStar_fullStar_fullStar_fullStar_full

@mxcl, @Devin: I fiddled wi...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
def hash_from_string string, value
  if key = string[/^([^.]+)/]
    {key => hash_from_string(string.gsub(/^[^.]+\.?/, ''), value)}
...

Ruby On Nested Hash from delimiter-...

by Martin Plöger, March 04, 2010 22:30

Use recursion on a recursiv...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
class String
  def squish
    self.gsub(/^.{#{self[/^\s+/].length}}/, '')
...

Ruby On Function to remove consiste...

by Martin Plöger, March 04, 2010 21:36 Star_fullStar_fullStar_fullStar_fullStar_full

You could also use ONLY a R...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
require 'date'
puts((Date.today..Date.today>>12).inject([]) do |m, d|
  d.day == 1 ? m.push(nil, d.strftime('%b'), nil) : (m << nil if d.wday == 1)
...

Ruby On Generate Days of a Year

by Martin Plöger, February 28, 2010 00:17

I'm using #inject to bu...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
puts (1..100).map { |n| [:fizzbuzz][n%15] || [:fizz][n%3] || [:buzz][n%5] || n }

Ruby On Fizz Buzz

by Martin Plöger, January 18, 2010 21:13 Star_fullStar_fullStar_fullStar_fullStar_full

You could also use 15 as th...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
puts (1..100).map { |n| ['fizzbuzz'][n%3+n%5] || ['fizz'][n%3] || ['buzz'][n%5] || n }

Ruby On Fizz Buzz

by Martin Plöger, January 18, 2010 20:40 Star_fullStar_fullStar_fullStar_fullStar_full
E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
def fizzbuzz max
  (1..max).map do |n|
    s = ['fizz'][n%3].to_s + ['buzz'][n%5].to_s
...

Ruby On Fizz Buzz

by Martin Plöger, January 16, 2010 12:35
E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
def increment_clicks!(hash_string)
  self.increment :clicks if (hash = self.advertisement_hashes.find_by_hash_string(hash_string)) && hash.destroy           # If you want to destroy the object
  # self.increment :clicks if self.advertisement_hashes.delete self.advertisement_hashes.find_by_hash_string(hash_string) # Otherwise
...

Ruby On Incrementing an attribute

by Martin Plöger, September 30, 2009 09:56

To simply increment an attr...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
def determine_layout
  case params[:action]
    when 'index', 'show', 'edit', 'update' then 'application'
...

Ruby On Determine layout

by Martin Plöger, September 14, 2009 12:15

Not that short, but IMHO mo...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
#...
case name
           when /female/ then 'Female Dorm'
...

Ruby On ungliness and long too

by Martin Plöger, September 11, 2009 09:16

I just updated my solution ...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
mapping = {'double' => ['Double Bed', 2], 'twin' => ['Twin Bed', 2], 'single' => ['Single Bed', 1], 'female' => 'Female Dorm', ... }

type, size = mapping.find { |k, v| name.include? k }
...

Ruby On ungliness and long too

by Martin Plöger, September 10, 2009 14:31 Star_fullStar_fullStar_full

Another way might be a mapp...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
stuff.each do |name|
  name = name.downcase
  
...

Ruby On ungliness and long too

by Martin Plöger, September 10, 2009 14:05 Star_fullStar_fullStar_fullStar_full

What do you exactly want wi...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
tmp = reply.case.users.map(&:email)
tmp.delete(reply.created_by.email)
tmp.join(',')           #ATTENTION DEAD CODE!!! DON'T NEED THAT tmp is NOT changed by this and the result is not used by you. result = tmp.join(',')...
...

Ruby On Remove element from array

by Martin Plöger, September 03, 2009 12:27 Star_fullStar_fullStar_fullStar_fullStar_full
E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk