小编Mor*_*ker的帖子

什么是JSON八位字节,为什么需要两个?

我有传入的数据,我存储在一个变量中messages:

connection = ContextIO::Connection.new(key, secret)
messages = connection.all_messages(:account => account, :limit => 100, :since => (Time.now - 3000.day ))
Run Code Online (Sandbox Code Playgroud)

变量messages以JSON格式化.然后我执行这个:

 foo = JSON.parse(messages)['data']
Run Code Online (Sandbox Code Playgroud)

大多数时候这是有效的.我一次又一次地收到此错误消息:

  A JSON text must at least contain two octets!  
Run Code Online (Sandbox Code Playgroud)

然后该错误消息引用该行 JSON.parse(messages)['data']

  1. 什么是八位字节?

  2. 为什么JSON文本必须包含至少两个八位字节?

  3. 如何防止我的代码每次messages都没有打破两个八位字节?

谢谢!

variables json ruby-on-rails

37
推荐指数
2
解决办法
2万
查看次数

Rails:从哈希数组中删除元素

我有以下数组:

 array = [{"email"=>"test@test.com", "name"=>"Test"},
          {"email"=>"testA@test.com", "name"=>"Test A"},
          {"name"=>"Test B", "email"=>"testB@test.com"},
          {"email"=>"testC@test.com", "name"=>"Test C"},
          {"name"=>"Test D", "email"=>"testD@test.com"},
          {"email"=>"testE@test.com"},
          {"name"=>"Test F", "email"=>"testF@test.com"}]
Run Code Online (Sandbox Code Playgroud)

我有一个"黑名单"电子邮件列表,例如:

 blacklist = ["testC@test.com"]
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情:

 array - blacklist 
 # => should remove element {"email"=>"testC@test.com", "name"=>"Test C"} 
Run Code Online (Sandbox Code Playgroud)

当然有一种性感的Ruby方式可以用.select或者其他东西来做到这一点,但是我无法弄明白.我试过这个无济于事:

 array.select {|k,v| v != "testC@test.com"} # => returns array without any changes
Run Code Online (Sandbox Code Playgroud)

ruby arrays hash ruby-on-rails

23
推荐指数
1
解决办法
2万
查看次数

在Ruby on Rails中正确使用REJECT方法 - 帮助我将许多行转换为Ruby单行

我有一系列散列名称和电子邮件,如下所示:

array = [{"email"=>"test@test.com", "name"=>"Test"},
      {"email"=>"testA@gmail.com", "name"=>"Test A"},
      {"name"=>"Test B", "email"=>"testB@test.com"},
      {"email"=>"testC@yahoo.com", "name"=>"Test C"},
      {"name"=>"Test D", "email"=>"testD@hotmail.com"},
      {"email"=>"testE@test.com"},
      {"name"=>"Test F", "email"=>"testF@test.com"}]
Run Code Online (Sandbox Code Playgroud)

我想过滤掉"黑名单"数组中的某些电子邮件.以下作品,但它太冗长了.

 blacklist = ["@test.com", "@gmail.com"] 
 na = array
 blacklist.each do |b|
   na = na.reject{ |e| e["email"].include?(b) }
 end

 # na => [{"email"=>"testC@yahoo.com", "name"=>"Test C"}, {"name"=>"Test D", "email"=>"testD@hotmail.com"}]
Run Code Online (Sandbox Code Playgroud)

有人可以通过把它变成性感的Ruby单行来帮助我吗?

ruby arrays hash ruby-on-rails

5
推荐指数
1
解决办法
3765
查看次数

如何在rails函数中创建动态变量?

我有这个代码正在工作:

  case index
   when "Books"
     @reading_img = res.items.first.get_hash('MediumImage')["URL"] # don't show an image
     @reading_link = create_amz_url(search, asin)        

     tempy = @nowreading.content.match(/#nowreading.*/).to_s.gsub("#nowreading",'') # strips away the #nowreading tag
     @nowreading.content = tempy.match(/#{search}.*/).to_s.gsub("#{search}", @reading_link) 
     # replaces the book title (passed in as variable 'search') with the URL'ed title

   when "Music"
     @listening_img = res.items.first.get_hash('MediumImage')["URL"] # don't show an image
     @listening_link = create_amz_url(search, asin)        

     tempy = @nowlistening.content.match(/#nowlistening.*/).to_s.gsub("#nowlistening",'') # strips away the #nowreading tag
     @nowlistening.content = tempy.match(/#{search}.*/).to_s.gsub("#{search}", @listening_link) 
     # replaces the song title (passed in as variable …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails dynamic

3
推荐指数
1
解决办法
4544
查看次数

走过字符串,根据名字字典从电子邮件中猜出一个名字?

假设我有一个名字词典(一个巨大的CSV文件).我想从一个没有明显可解析点(., - ,_)的电子邮件中猜出一个名字.我想做这样的事情:

  dict = ["sam", "joe", "john", "parker", "jane", "smith", "doe"]
  word = "johnsmith"
  x = 0
  y = word.length-1
  name_array = []
  for i in x..y
     match_me = word[x..i]
     dict.each do |name|
       if match_me == name
         name_array << name
       end
     end
  end   

  name_array
  # => ["john"]
Run Code Online (Sandbox Code Playgroud)

不错,但我想要"约翰史密斯"或["约翰","史密斯"]

换句话说,我递归循环遍历单词(即未解析的电子邮件字符串,"johndoe@gmail.com"),直到我在字典中找到匹配项. 我知道:这非常低效. 如果有一个更简单的方法,我会全力以赴!

如果没有更好的方法,那么请告诉我如何解决上面的例子,因为它有两个主要缺陷:(1)如何设置循环的长度(参见下面找到"i"的问题), (2)如何在上面的例子中增加"x",以便我可以在给定任意字符串的情况下循环遍历所有可能的字符组合?

找到循环长度的问题,"i":

  for an arbitrary word, how can we derive "i" given the pattern below?

  for a (i = 1)
  a

  for ab (i = 3)
  a …
Run Code Online (Sandbox Code Playgroud)

ruby loops ruby-on-rails email-validation pattern-matching

1
推荐指数
1
解决办法
270
查看次数