ruby

Repeat characters in a string

拟墨画扇 提交于 2021-01-28 18:20:32
问题 I know this is a simple problem, but for some reason it wont click. I want to repeat characters in a string for a fixed amount. For example: str = 'abcde' temp = '' x = 8 for i in 0..x if str[i].nil? temp << '' else temp << str[i] end end Except I get no input. What I need is: abcdeabc Please, any help would be appreciated. If there is a better working way to do this instead of my not working naive approach, I would like to know 回答1: Using ljust should do it: str = 'abcde' str.ljust(8, str) #

Using array values as hash keys to create nested hashes in Ruby

一笑奈何 提交于 2021-01-28 17:51:47
问题 Background I have a directory of files. The majority of them are .patch files, although some of them are not. For each patch file, I need to scan the first line which is always a string and extract a portion of that line. With a combination of each file's name, that portion of each first line, and a unique identifier for each file, I need to create a hash which I will then convert to json and write to a file. Here are examples: Directory of Files (Example) |__ .gitkeep |__ pmet-add-install

How to properly mock itnernal services in RSpec?

混江龙づ霸主 提交于 2021-01-28 14:41:36
问题 I would like to learn how to properly mock object calls inside other classes, foor example I have this controller action: def show service = Action::PartsShow.new(show_params, current_user) service.call render json: service.part, root: :part, serializer: PartSerializer, include: '**', scope: {current_user: current_user} end The service class looks like this. module Action class PartsShow < PartsShowBase def find_part ... end end end module Action class PartsShowBase attr_reader :part def

How to properly mock itnernal services in RSpec?

我是研究僧i 提交于 2021-01-28 14:35:27
问题 I would like to learn how to properly mock object calls inside other classes, foor example I have this controller action: def show service = Action::PartsShow.new(show_params, current_user) service.call render json: service.part, root: :part, serializer: PartSerializer, include: '**', scope: {current_user: current_user} end The service class looks like this. module Action class PartsShow < PartsShowBase def find_part ... end end end module Action class PartsShowBase attr_reader :part def

Switch to inspect mode. - WIndows 10

Deadly 提交于 2021-01-28 14:24:44
问题 i got a prompt "Switch to inspect mode." when i do rails c it shows Loading development environment (Rails 6.0.3.4) Switch to inspect mode. and I can't do anything only type "exit" to exit this console but I'd like to check last post in my website by typing @post = Post.last when i type irb it shows me the same. ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x64-mingw32] git-bash I don't know what to do, I've tried to find a solution but futilely. Am I supposed to install linux to learn how

String becomes empty passing through FFI from rust to ruby

烈酒焚心 提交于 2021-01-28 13:55:36
问题 I have a rubygem with a native extension written in rust. The native extension supports serializing its data-structure to JSON. However, whilst I've confirmed it's generating JSON, the string is always empty on the ruby side. Here's the ruby code: module RustCuckooFilter extend FFI::Library ffi_lib 'libcuckoofilter_cabi' class Instance < FFI::AutoPointer def export(path) RustCuckooFilter.export(self) end end attach_function :export, :rcf_cuckoofilter_export, [Instance], :pointer And the rust

How to show input field error message in rails input field

白昼怎懂夜的黑 提交于 2021-01-28 13:50:40
问题 Error message not showing only field highlighted <%= f.text_field :website,:required=>true,:pattern=>'https?://.+' %> How I show that? 回答1: Option 1 (recommended): use simple_form gem. It makes it easy to display error messages next to the fields. After installing the gem, you can then simply do this: <%= f.input :website %> Option 2: code it yourself. Something like the following would be a start. You'll probably want to add some CSS classes for styling, and decide what to do if multiple

How to read a GZIP payload in Ruby Sinatra

烂漫一生 提交于 2021-01-28 12:23:10
问题 On a remote host I have a bash script sending a simple gzipped YAML file to my Ruby Sinatra endpoint: #!/bin/bash /bin/gzip -c /tmp/test.yaml > /tmp/test.gz curl -i <hostname>:<port>/last_run_report -H "Content-Type: application/xml" -H "Content-Encoding: gzip" --data-binary @/tmp/test.gz My sample Ruby app is: require 'sinatra' require 'zlib' require 'stringio' set :port, <port> set :bind, "<ip>" post '/last_run_report' do sio = StringIO.new(request.body.to_s) gz = Zlib::GzipReader.new(sio)

Ruby unpack binary

亡梦爱人 提交于 2021-01-28 11:23:55
问题 I am working on unpacking a binary file for the first time in Ruby. Already found the unpack method which works pretty nice. Which according to the docs works perfect for 8(1 byte),16(2 byte),32(4 byte) and 64 bit(8 byte). But now I have to unpack 5 bytes. How do I do this? Thx in advance! 回答1: To literally unpack five bytes: str.unpack 'C5' That gives you five byte values as unsigned ints. The question is how to reinterpret those ints as a single data type. Pack/unpack only recognize the

Remove Coupon from Stripe Subscription in Update

◇◆丶佛笑我妖孽 提交于 2021-01-28 11:18:24
问题 I need to remove a coupon on a subscription during update, I thought passing coupon of nil to the api should remove it, but it just removes it from the post. There is another way of doing it like this.. https://stripe.com/docs/api/discounts/subscription_delete but it requires another call I don't want to do. Ruby Stripe Gem API: Stripe::Subscription.update( subscription.stripe_id, { coupon: nil, items: [ { id: subscription.item_stripe_id, quantity: 0, }, { plan: to_plan.stripe_id, quantity: 1