[ create a new paste ] login | about

Link: http://codepad.org/pVLdRM5J    [ raw code | output | fork ]

Ruby, pasted on May 16:
#!/usr/bin/env ruby

def stringThisWay
  result = 'test'
  return result
end

def stringThatWay
  'test'
end

puts stringThisWay
puts stringThatWay

def hashThisWay
  result = Hash.new
  [ 'a', 'b', 'c' ].each do |var|
    result[var.to_sym] = var
  end
  return result
end

def hashThatWay
  Hash.new.tap do |result|
    [ 'a', 'b', 'c' ].each do |var|
      result[var.to_sym] = var
    end
  end
end

puts hashThisWay.inspect
puts hashThatWay.inspect


Output:
1
2
3
4
5
Line 24:in `hashThatWay': undefined method `tap' for {}:Hash (NoMethodError)
	from t.rb:32
test
test
{:b=>"b", :c=>"c", :a=>"a"}


Create a new paste based on this one


Comments: