ruby

Converting an empty string into nil in Ruby

夙愿已清 提交于 2021-02-05 20:20:11
问题 I have a string called word and a function called infinitive such that word.infinitive would return another string on some occasions and an empty string otherwise I am trying to find an elegant ruby one line expression for the code-snippet below if word.infinitive == "" return word else return word.infinitive Had infinitive returned nil instead of "", I could have done something like (word.infinitive or word) But since it does not, I can't take advantage of the short-circuit OR Ideally I

Sinatra and Grape API together?

五迷三道 提交于 2021-02-05 20:16:32
问题 I've been reading around and I found this micro-framework called Grape for ruby. I am currently using Sinatra to handle the web interface but I would also like to implement Grape to handle the API aspect of the app. I can't find any helpful suggestions to this topic. The grape documentation says "Grape is a REST-like API micro-framework for Ruby. It's designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily develop

Sinatra and Grape API together?

≡放荡痞女 提交于 2021-02-05 20:11:40
问题 I've been reading around and I found this micro-framework called Grape for ruby. I am currently using Sinatra to handle the web interface but I would also like to implement Grape to handle the API aspect of the app. I can't find any helpful suggestions to this topic. The grape documentation says "Grape is a REST-like API micro-framework for Ruby. It's designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily develop

Compressing large string in ruby

吃可爱长大的小学妹 提交于 2021-02-05 13:40:24
问题 I have a web application(ruby on rails) that sends some YAML as the value of a hidden input field. Now I want to reduce the size of the text that is sent across to the browser. What is the most efficient form of lossless compression that would send across minimal data? I'm ok to incur additional cost of compression and decompression at the server side. 回答1: You could use the zlib implementation in the ruby core to in/de-flate data: require "zlib" data = "some long yaml string" * 100

How to import without specifing path in ruby

偶尔善良 提交于 2021-02-05 11:36:32
问题 When I develop ruby app. I found that some apps import other modules without specifying specific path like following require 'test' I always set some modules in certain directory and set following path. require './sample/test' How can I import without path ? Are there any environment setting around this? Am I missing important thing? If someone has opinion,please let me know. Thanks 回答1: As was mentioned in the comments, Ruby looks at the $LOAD_PATH global variable to know where to look for

`initialize': wrong number of arguments (given 0, expected 2) (ArgumentError)

北城余情 提交于 2021-02-05 10:48:06
问题 class LineAnalyzer @@highest_wf_count=[] @@highest_wf_words=[] attr_accessor :highest_wf_count ,:highest_wf_words ,:content , :line_number def initialize(line,num) @content=line @line_number=num calculate_word_frequency(@content,@line_number).call end def calculate_word_frequency(con,num) @content,@line_number=con,num @arr= @content.split() @arr.map do |txt| @count=0 @i=0 while @i<@content.length @count+=1 if txt.eql?(@arr[@i]) @i+=1 end @@highest_wf_count[@line_number]= @count @@highest_wf

Refactoring Pokemon type weekness

佐手、 提交于 2021-02-05 10:46:46
问题 I made pokemon weekness finder by ruby. It takes a lot of words. Could you suggest bit nice and sophisticated to write this code? The menu is as follows. puts "Which type of Pokemon do you want to know weaknesses? Menu 1 Normal 2 Fire 3 Water 4 Electric 5 Grass 6 Ice 7 Fighting 8 Poison 9 Ground 10 Flying 11 Psychic 12 Bug 13 Rock 14 Ghost 15 Dragon 16 Dark 17 Steel 18 Fairy I then obtain the user's response and print the weakness for their selection. type = gets.to_i case type when 1 #Normal

Delete from database with specific details in ruby

扶醉桌前 提交于 2021-02-05 10:46:45
问题 I try to delete from database all row has a id include in some array or (key,value) "recp" => "1, 2, 3 , 6, 7 , ........." ID in @recipient I try this: @v = NameOfDatabase.where.not(:id=> @recipient.split(',').map(&:to_i), :conditions => {:thread =>@dis_PI.m_id}).destroy_all With specific condition i want to remove row with this condition and not include in @recipient Error in this method : NoMethodError (undefined method `where' for #<Class:0x7f447f57b140>): I try multiple code but not

Refactoring Pokemon type weekness

寵の児 提交于 2021-02-05 10:45:51
问题 I made pokemon weekness finder by ruby. It takes a lot of words. Could you suggest bit nice and sophisticated to write this code? The menu is as follows. puts "Which type of Pokemon do you want to know weaknesses? Menu 1 Normal 2 Fire 3 Water 4 Electric 5 Grass 6 Ice 7 Fighting 8 Poison 9 Ground 10 Flying 11 Psychic 12 Bug 13 Rock 14 Ghost 15 Dragon 16 Dark 17 Steel 18 Fairy I then obtain the user's response and print the weakness for their selection. type = gets.to_i case type when 1 #Normal

Yielding partitions of a multiset with Ruby

社会主义新天地 提交于 2021-02-05 07:16:16
问题 I would like to get all the possible partitions (disjoint subsets of a set which union is the original set) of a multiset (some elements are equal and non-distinguishable from each other). Simpler case when one would like to yield the partitions of a simple set, in which there are no elements with multiplicity, in other words all elements are different. For this scenario I found this Ruby code on StackOwerflow which is very efficient, as not storing all the possible partitions, but yielding