ruby-1.8

Ruby 1.8: Hash#sort not return hash but array (better way to do this?)

非 Y 不嫁゛ 提交于 2019-12-01 11:09:20
问题 In some scenario of Ruby 1.8. If I have a hash # k is name, v is order foo = { "Jim" => 1, "bar" => 1, "joe" => 2} sorted_by_values = foo.sort {|a, b| a[1] <==> b[1]} #sorted_by_values is an array of array, it's no longer a hash! sorted_by_values.keys.join ',' my workaround is to make method to_hash for Array class. class Array def to_hash(&block) Hash[*self.collect { |k, v| [k, v] }.flatten] end end I can then do the following: sorted_by_values.to_hash.keys.join ',' Is there a better way to

Convert time to other timezone

我怕爱的太早我们不能终老 提交于 2019-11-28 12:42:35
I have a datestring in this format yyyy-mm-ddThh:mm:ss[Z] And i have a timezone string. for e.g "Asia/Kolkata" Now i want to convert this date string into the timezone of the given timezone for e.g. if the date is 2014-01-03T23:30:00Z , then in "Asia/Kolkata" timezone it will be 2014-01-04T05:00:00 . I tried using Time library , but Time library does not seem to have any method which can convert to other timzone http://ruby-doc.org/core-1.8.6/Time.html#method-c-mktime . You should use the TZInfo gem. require 'tzinfo' tz = TZInfo::Timezone.get('Asia/Kolkata') utc = DateTime.iso8601('2014-01

Supporting Ruby 1.9's hash syntax in Ruby 1.8

对着背影说爱祢 提交于 2019-11-27 22:33:08
I'm writing a Ruby gem using the {key: 'value'} syntax for hashes throughout my code. My tests all pass in 1.9.x, but I (understandably) get syntax error, unexpected ':', expecting ')' in 1.8.7. Is there a best practice for supporting the 1.8.x? Do I need to rewrite the code using our old friend => , or is there a better strategy? I think you're out of luck, if you want to support 1.8 then you have to use => . As usual, I will mention that you must use => in certain cases in 1.9: If the key is not a symbol. Remember that any object (symbols, strings, classes, floats, ...) can be a key in a

(Ruby) Getting Net::SMTP working with Gmail…?

谁都会走 提交于 2019-11-27 21:11:02
Does anyone have any quality (and up-to-date) information regarding sending mail via Gmail using Ruby's Net::SMTP? I've seen several examples -- most dating from 2007 to mid-2008 and none of them work for me. I need more current examples that use the most recent 1.8.7 release. I'd also appreciate if the documentation didn't only cover simple examples that no one ever really uses. Currently I'm receiving an error: SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol I'm not terribly familiar with SSL as regards the SMTP protocol, so this is all lost on me.

Convert time to other timezone

雨燕双飞 提交于 2019-11-27 07:12:10
问题 I have a datestring in this format yyyy-mm-ddThh:mm:ss[Z] And i have a timezone string. for e.g "Asia/Kolkata" Now i want to convert this date string into the timezone of the given timezone for e.g. if the date is 2014-01-03T23:30:00Z , then in "Asia/Kolkata" timezone it will be 2014-01-04T05:00:00 . I tried using Time library , but Time library does not seem to have any method which can convert to other timzone http://ruby-doc.org/core-1.8.6/Time.html#method-c-mktime . 回答1: You should use

Supporting Ruby 1.9's hash syntax in Ruby 1.8

妖精的绣舞 提交于 2019-11-26 23:10:50
问题 I'm writing a Ruby gem using the {key: 'value'} syntax for hashes throughout my code. My tests all pass in 1.9.x, but I (understandably) get syntax error, unexpected ':', expecting ')' in 1.8.7. Is there a best practice for supporting the 1.8.x? Do I need to rewrite the code using our old friend => , or is there a better strategy? 回答1: I think you're out of luck, if you want to support 1.8 then you have to use => . As usual, I will mention that you must use => in certain cases in 1.9: If the

(Ruby) Getting Net::SMTP working with Gmail…?

試著忘記壹切 提交于 2019-11-26 20:33:51
问题 Does anyone have any quality (and up-to-date) information regarding sending mail via Gmail using Ruby's Net::SMTP? I've seen several examples -- most dating from 2007 to mid-2008 and none of them work for me. I need more current examples that use the most recent 1.8.7 release. I'd also appreciate if the documentation didn't only cover simple examples that no one ever really uses. Currently I'm receiving an error: SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown

Ruby: require vs require_relative - best practice to workaround running in both Ruby <1.9.2 and >=1.9.2

只谈情不闲聊 提交于 2019-11-26 19:17:47
What is the best practice if I want to require a relative file in Ruby and I want it to work in both 1.8.x and >=1.9.2? I see a few options: just do $LOAD_PATH << '.' and forget everything do $LOAD_PATH << File.dirname(__FILE__) require './path/to/file' check if RUBY_VERSION < 1.9.2, then define require_relative as require , use require_relative everywhere where it's needed afterwards check if require_relative already exists, if it does, try to proceed as in previous case use weird constructions such as require File.join(File.dirname(__FILE__), 'path/to/file') - alas they don't seem to work in

Ruby: require vs require_relative - best practice to workaround running in both Ruby <1.9.2 and >=1.9.2

你离开我真会死。 提交于 2019-11-26 06:52:24
问题 What is the best practice if I want to require a relative file in Ruby and I want it to work in both 1.8.x and >=1.9.2? I see a few options: just do $LOAD_PATH << \'.\' and forget everything do $LOAD_PATH << File.dirname(__FILE__) require \'./path/to/file\' check if RUBY_VERSION < 1.9.2, then define require_relative as require , use require_relative everywhere where it\'s needed afterwards check if require_relative already exists, if it does, try to proceed as in previous case use weird

What is the difference between Ruby 1.8 and Ruby 1.9

亡梦爱人 提交于 2019-11-26 06:13:27
问题 I\'m not clear on the differences between the \"current\" version of Ruby (1.8) and the \"new\" version (1.9). Is there an \"easy\" or a \"simple\" explanation of the differences and why it is so different? 回答1: Sam Ruby has a cool slideshow that outline the differences. In the interest of bringing this information inline for easier reference, and in case the link goes dead in the abstract future, here's an overview of Sam's slides. The slideshow is less overwhelming to review, but having it