Changing every value in a hash in Ruby
I want to change every value in a hash so as to add ‘%’ before and after the value so { :a=>’a’ , :b=>’b’ } must be changed to { :a=>’%a%’ , :b=>’%b%’ } What’s the best way to do this? 11 Answers 11
I want to change every value in a hash so as to add ‘%’ before and after the value so { :a=>’a’ , :b=>’b’ } must be changed to { :a=>’%a%’ , :b=>’%b%’ } What’s the best way to do this? 11 Answers 11
I just uninstalled my older versions of Ruby, removed all of my gems (including Rails), and installed Ruby 2.0. In other words, a totally clean re-install. Upon starting IRB, I received this message: DL is deprecated, please use Fiddle Note: I’m on a Windows machine. What does this message mean? 5 Answers 5
This question already has answers here: How to chunk an array in Ruby (2 answers) Closed 4 years ago. I have an array foo = %w(1 2 3 4 5 6 7 8 9 10) How can I split or “chunk” this into smaller arrays? class Array def chunk(size) # return array of arrays end … Read more
Is there a specific time when I should use “” vs ”? I’ve been using single quotes most of the time because it’s easier to type but I’m not sure if I should. e.g. get ‘user/new’ vs. get “user/new” 7 Answers 7
Ruby’s File.open takes modes and options as arguments. Where do I find a complete list of modes and options? 2 Answers 2
I can run all tests in a single file with: rake test TEST=path/to/test_file.rb However, if I want to run just one test in that file, how would I do it? I’m looking for similar functionality to: rspec path/to/test_file.rb -l 25 15 Answers 15
On both my work and home computers, I recently upgraded Ruby to 2.3.1, using ruby-install. I use chruby as my Ruby switcher. I started seeing this warning in my terminal: Ignoring bcrypt-3.1.11 because its extensions are not built. Try: gem pristine bcrypt –version 3.1.11 Ignoring bcrypt-3.1.10 because its extensions are not built. Try: gem pristine … Read more
This is what I have now – which looks too verbose for the work it is doing. @title = tokens[Title].strip! || tokens[Title] if !tokens[Title].nil? Assume tokens is a array obtained by splitting a CSV line. now the functions like strip! chomp! et. all return nil if the string was not modified “abc”.strip! # => nil … Read more
I have a ruby array like [’12’,’34’,’35’,’231′]. I want to convert it to a string like ’12’,’34’,’35’,’231′. How can I do that? 13 Answers 13