ruby-1.9

Is there a way in ruby 1.9 to remove invalid byte sequences from strings?

ε祈祈猫儿з 提交于 2019-11-28 07:19:59
Suppose you have a string like "€foo\xA0" , encoded UTF-8, Is there a way to remove invalid byte sequences from this string? ( so you get "€foo" ) In ruby-1.8 you could use Iconv.iconv('UTF-8//IGNORE', 'UTF-8', "€foo\xA0") but that is now deprecated. "€foo\xA0".encode('UTF-8') doesn't do anything, since it is already UTF-8. I tried: "€foo\xA0".force_encoding('BINARY').encode('UTF-8', :undef => :replace, :replace => '') which yields "foo" But that also loses the valid multibyte character € Evgenii "€foo\xA0".chars.select(&:valid_encoding?).join Van der Hoorn "€foo\xA0".encode('UTF-16le',

How do I get the class of a BasicObject instance?

故事扮演 提交于 2019-11-28 04:44:25
I have a script that iterates using ObjectSpace#each_object with no args. Then it prints how many instances exist for each class. I realized that some classes redefine the #class instance method, so I had to find another way to get the actual class; Let's say it's stored in variable "klass" , and klass === object is true. In Ruby 1.8 I could do this, assuming Object wasn't monkeypatched: Object.instance_method(:class).bind(object).call This worked for ActiveSupport::Duration instances: # Ruby 1.8 # (tries to trick us) 20.seconds.class => Fixnum # don't try to trick us, we can tell Object

What's the difference between Object and BasicObject in Ruby?

南楼画角 提交于 2019-11-27 21:01:17
What's the difference between these classes? What's the difference between their purposes? Aliaksei Kliuchnikau BasicObject was introduced in Ruby 1.9 and it is a parent of Object (thus BasicObject is the parent class of all classes in Ruby). BasicObject has almost no methods on itself: ::new #! #!= #== #__id__ #__send__ #equal? #instance_eval #instance_exec BasicObject can be used for creating object hierarchies independent of Ruby's object hierarchy, proxy objects like the Delegator class, or other uses where namespace pollution from Ruby's methods and classes must be avoided. BasicObject

Ruby 1.9 hash with a dash in a key

给你一囗甜甜゛ 提交于 2019-11-27 20:21:23
In ruby 1.9 is there a way to define this hash with the new syntax? irb> { a: 2 } => {:a=>2} irb> { a-b: 2 } SyntaxError: (irb):5: syntax error, unexpected tLABEL { a-b: 2 } ^ with the old one, it's working: irb> { :"a-b" => 2 } => {:"a-b"=>2} You also can use next syntax {a: 1, b: 2, 'c-c': 3, d: 4} There are some legitimate symbols that cannot be used with the new syntax. I cannot find a reference, but it appears that a symbol name matching /^[a-zA-Z_][a-zA-Z_0-9]*[!?]?$/ is allowed with the new syntax. The last character may be the special character "!" or "?". For any symbol that does not

What's the difference between Process.fork and Process.spawn in Ruby 1.9.2

感情迁移 提交于 2019-11-27 20:02:11
问题 What's the difference between Process.fork and the new Process.spawn methods in Ruby 1.9.2 and which one is better to run another program in a subprocess? As far as I understand Process.fork accepts block of code and Process.spawn takes a system command plus some other parameters. When I should use one instead of the other? 回答1: What's the difference between Process.fork and the new Process.spawn methods in Ruby 1.9.2 Process.fork allows you to run ruby code in another process. Process.spawn

How do I reference a function in Ruby?

点点圈 提交于 2019-11-27 19:03:22
In python, it's fairly straightforward to reference a function: >>> def foo(): ... print "foo called" ... return 1 ... >>> x = foo >>> foo() foo called 1 >>> x() foo called 1 >>> x <function foo at 0x1004ba5f0> >>> foo <function foo at 0x1004ba5f0> However, it seems to be different in Ruby since a naked foo actually calls foo: ruby-1.9.2-p0 > def foo ruby-1.9.2-p0 ?> print "foo called" ruby-1.9.2-p0 ?> 1 ruby-1.9.2-p0 ?> end => nil ruby-1.9.2-p0 > x = foo foo called => 1 ruby-1.9.2-p0 > foo foo called => 1 ruby-1.9.2-p0 > x => 1 How do I actually assign the function foo to x and then call it?

How do I make Ruby 1.9 the default Ruby on Ubuntu?

老子叫甜甜 提交于 2019-11-27 17:26:19
Is there any way, on Ubuntu 9.04, to install Ruby 1.8 as ruby1.8 (or get rid of it altogether) and have Ruby 1.9 be the default ruby ? just_a_dude I'm not really sure, but maybe this can help: update-alternatives --config ruby ... and here's the non-interactive, scriptable, way: update-alternatives --set ruby /usr/bin/ruby1.9.1 You may find out about available alternatives and respective /usr/bin/... paths by doing: update-alternatives --query ruby henry74 Martin - Take a look at the following link: http://ryanbigg.com/2010/12/ubuntu-ruby-rvm-rails-and-you/ This is where installation of ruby

Why are all strings ASCII-8BIT after I upgraded to Rails 3?

不羁的心 提交于 2019-11-27 16:37:14
问题 I upgraded to RoR 3.0.1 and Ruby to 1.9.2. Now all the strings in my views are ASCII-8BIT? I believe I have my app set up to use UTF 8 application.rb config.encoding = "utf-8" database.yml development: adapter: mysql encoding: utf8 I'm running OS X RVM rvm 1.0.16 Ruby ruby-1.9.2-p0 Rails 3.0.1 I'd expect that the enoding would be UTF 8 not ASCII business.desc.encoding # ASCII-8BIT Since 1.9.x can concatenate strings of different encodings we see a lot of errors like this. <p class=

What are the benefits of the new hash syntax in Ruby 1.9?

北城余情 提交于 2019-11-27 16:01:21
Apart from making it sightly more concise for defining hashes with symbols as keys, are there any other benefits of writing a hash as: {key1: "value1", key2: "value2"} instead of {:key1 => "value1", :key2 => "value2"} ? Also, what is the convention when you have a mix of strings and symbols as hash keys? Do you write it as {"key1" => "value1", key2: "value2"} or keep the style consistant as {"key1" => "value1", :key => "value2"} It just looks nicer--it's syntactic sugar; it ends up being the same thing. When mixing keys (ew, why would you do that?) I use the old hash-rocket syntax for the

How to replace the Unicode gem on Ruby 1.9?

北慕城南 提交于 2019-11-27 12:35:45
问题 Unfortunately, the Unicode 0.1 ( sudo gem install unicode ) doesn't work on Ruby 1.9. I have the following snippet: require "rubygems" require "unicode" str = "áéíóúç" Unicode.normalize_KD(str).gsub(/[^\x00-\x7F]/n, "") #=> aeiouc I use it to convert titles to permalink, without removing accented characters. Is there a way of converting such texts using pack or unpack methods? 回答1: Update: a better option may be to use the gem unicode_utils that was created specifically for these missing