1
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
1
2
3
4
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
1
2
3
4
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
1
2
3
4
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
1
2
3
4
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 build ...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
1
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
1
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
1
2
3
4
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
1
2
3
4
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
1
2
3
4
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
1
2
3
4
#...
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
1
2
3
4
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

Another way might be a mapp...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
1
2
3
4
stuff.each do |name|
  name = name.downcase
  
...

Ruby On ungliness and long too

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

What do you exactly want wi...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
1
2
3
4
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
1
2
3
4
def is_18?
  RUBY_VERSION =~ /1.8/
end
...

Ruby On Running Ruby 1.9 or 1.8 code

by Martin Plöger, August 25, 2009 07:38 Star_fullStar_fullStar_fullStar_fullStar_full
E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
1
2
3
4
if string.include? '1'
  1
else
...

Ruby On Case statement with Regular...

by Martin Plöger, August 20, 2009 12:33 Star_fullStar_fullStar_fullStar_fullStar_full

If the given example has a ...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk

Ruby On Convert deep hash into a st...

by Martin Plöger, August 07, 2009 09:21

@Desty Nova: Really nice, a...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
1
2
3
def toll_free_dealer_phone phone
  phone.gsub /^(866|877|888)/, '1-\1'
end

Ruby On Add 1- in front of a number

by Martin Plöger, July 27, 2009 13:39

carlpelletier.myopenid.com:...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
1
2
3
4
def convert x
  x.keys.collect do |k|
    [convert(x[k])].flatten.collect { |j| "#{k}#{".#{j}" if j}" }
...

Ruby On Convert deep hash into a st...

by Martin Plöger, July 27, 2009 12:59

Hope this is right and read...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk

Ruby On shorten long variable names...

by Martin Plöger, June 23, 2009 15:04

Why do you pass params[:pos...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
1
2
#OLD: @myvocabulary.vocabulary.lesson.proficiency.id
@myvocabulary.lession_proficiency #New: Introduce a method that delegates internally to the proficiency of the lesson

Ruby On shorten long variable names...

by Martin Plöger, June 23, 2009 08:35

You should not chain method...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
1
2
3
4
class Array
  def intersperse object
    zip(Array.new(length, object)).flatten[0...-1]
...

Ruby On Ruby: array intersperse, ef...

by Martin Plöger, June 21, 2009 17:43

This works too, but I don't...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
1
2
3
4
class Array
  def intersperse(object)
    collect { |element| [ element, object ] }.flatten[0...-1]
...

Ruby On Ruby: array intersperse, ef...

by Martin Plöger, June 21, 2009 08:33

You could also use the alia...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk
1
2
3
def current_snapshot
  instances.find { |instance| instance.id == self.current }
end

Ruby On avoid for loops

by Martin Plöger, June 20, 2009 18:26 Star_fullStar_fullStar_fullStar_fullStar_full

You could also use the alia...

E8f0d6e9bc8bca695b3c5bdf75cdcc03 Talk