Ruby: Creating a hash key and value from a variable in Ruby [duplicate]

If you want to populate a new hash with certain values, you can pass them to Hash::[]: Hash[“a”, 100, “b”, 200] #=> {“a”=>100, “b”=>200} Hash[ [ [“a”, 100], [“b”, 200] ] ] #=> {“a”=>100, “b”=>200} Hash[“a” => 100, “b” => 200] #=> {“a”=>100, “b”=>200} So in your case: Hash[id, ‘foo’] Hash[[[id, ‘foo’]]] Hash[id => ‘foo’] … Read more

In Ruby, how do I make a hash from an array?

Say you have a function with a funtastic name: “f” def f(fruit) fruit + “!” end arr = [“apples”, “bananas”, “coconuts”, “watermelons”] h = Hash[ *arr.collect { |v| [ v, f(v) ] }.flatten ] will give you: {“watermelons”=>”watermelons!”, “bananas”=>”bananas!”, “apples”=>”apples!”, “coconuts”=>”coconuts!”} Updated: As mentioned in the comments, Ruby 1.8.7 introduces a nicer syntax for this: … Read more

PHP short hash like URL-shortening websites

TinyURL doesn’t hash anything, it uses Base 36 integers (or even base 62, using lower and uppercase letters) to indicate which record to visit. Base 36 to Integer: intval($str, 36); Integer to Base 36: base_convert($val, 10, 36); So then, instead of redirecting to a route like /url/1234 it becomes /url/ax instead. This gives you a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)