i3 window manager – How do i create shortcut to execute ruby script in i3?

This is a dummy ruby script named ‘rr’ that i’m trying to get i3 to execute with a shortcut

#!/usr/bin/env ruby
File.write('/home/anu/asdf', 'mojojojo')

inside .config/i3/config –

bindsym Control+Mod1+w exec --no-startup-id "/home/anu/rr"

If i make an equivalent bash script then it works.

ruby – Rails: What route to let show one resource but submit to another?

I’m sketching out a data model for a Rails app that lets users submit answers to technical questions. For example, the prompt might be:

Write a SQL query to determine the number of unique visitors last week

My models would look like this:

class User < ApplicationRecord
  has_many :submissions

class Submission < ApplicationRecord
  belongs_to :user
  belongs_to :question

class Question < ApplicationRecord
  has_many :submissions

I’d like a user to be able to visit a page that will display:

  1. The question’s prompt
  2. The user’s previous submissions for this question
  3. A form that lets the user create a new submission for this question

How should I set up my routes given these requirements? I’m pretty lost but am thinking I could use nested resources in my routes.rb:

# not sure order matters here; I've usually seen nested routes be 
# ordered by the has many relationship.  so if A has many B and B has 
# many C, then the nesting order is A, B, C.

resources :questions do  
  resources :users do
    resources :submissions

If I did that, a specific user submitting an answer to a specific question could be:

POST to /questions/:question_id/users/:user_id/submissions

Then my controller’s create action could look like this:

class SubmissionController < ApplicationController
  def create
    @question = Question.find(params(:question_id))
    @user = User.find(params(:user_id))
    Submission.create(params(:submission), user: current_user, question: @question)
    @submissions = Submission.where(user: @user, question: @question)
    redirect_to (@question, @user)

Is there a more RESTful / Rails-ey way to go about the above? If there’s not, does the ordering of my nested routes matter? Thanks in advance for any help you can offer!

Staff Needed – ruby package please. | Forum Promotion

Ruby Package – VGR | Forum Promotion

Staff Needed – Ruby for DH | Forum Promotion

How to create Ruby Object from C Structure

I am trying to make a Ruby module using the C API.
I must admit, I am having trouble fully understanding the documentation online for it, but I am trying to return a Ruby Object using data from a C Structure from another class method (sorry if that doesn’t make sense). Here is an example of my problem:


#include "ruby.h"
#include "extconf.h"

typedef struct example1_t
    int x;
} example1_t;

typedef struct example2_t
    char *name;
} example2_t;

void example1_free(example1_t *e1);
void example2_free(example2_t *e2);

static VALUE rb_example1_alloc(VALUE klass)
    return Data_Wrap_Struct(klass, NULL, example1_free, ruby_xmalloc(sizeof(example1_t)));

static VALUE rb_example1_init(VALUE self, VALUE x)
    example1_t *e1;

    Check_Type(x, T_FIXNUM);

    Data_Get_Struct(self, example1_t, e1);

    e1->x = NUM2INT(x);

    return self;

static VALUE rb_example1_x(VALUE self)
    example1_t *e1;

    Data_Get_Struct(self, example1_t, e1);

    return INT2NUM(e1->x);

static VALUE rb_example2_alloc(VALUE klass)
    return Data_Wrap_Struct(klass, NULL, example2_free, ruby_xmalloc(sizeof(example2_t)));

static VALUE rb_example2_init(VALUE self, VALUE s)
    example2_t *e2;

    Check_Type(s, T_STRING);

    Data_Get_Struct(self, example2_t, e2);

    e2->name = (char*)malloc(RSTRING_LEN(s) + 1);
    memcpy(e2->name, StringValuePtr(s), RSTRING_LEN(s) + 1);

    return self;

static VALUE rb_example2_name(VALUE self)
    example2_t *e2;

    Data_Get_Struct(self, example2_t, e2);

    return rb_str_new_cstr(e2->name);

static VALUE rb_example2_name_len(VALUE self)
    example1_t *len;
    example2_t *e2;

    Data_Get_Struct(self, example2_t, e2);

    len->x = strlen(e2->name);


    How do I make a new Example1 Ruby Class from the "len" 
    structure and return it with the length of e2->name 
    assigned to len->x?

 return it?

void Init_example()
    VALUE mod = rb_define_module("Example");
    VALUE example1_class = rb_define_class_under(mod, "Example1", rb_cObject);
    VALUE example2_class = rb_define_class_under(mod, "Example2", rb_cObject);

    rb_define_alloc_func(example1_class, rb_example1_alloc);
    rb_define_alloc_func(example2_class, rb_example2_alloc);

    rb_define_method(example1_class, "initialize", rb_example1_init, 1);
    rb_define_method(example1_class, "x", rb_example1_x, 0);

    rb_define_method(example2_class, "initialize", rb_example2_init, 1);
    rb_define_method(example2_class, "name", rb_example2_name, 0);
    rb_define_method(example2_class, "name_len", rb_example2_name_len, 0);

void example1_free(example1_t *e1)
    memset(e1, 0, sizeof(example1_t));

void example2_free(example2_t *e2)
    memset(e2, 0, sizeof(example2_t));

As you can see in the rb_example2_name_len, I would like to create an Example1 class and return that from an Example2 method. How would I be able to do this?

Any help is much appreciated.

ruby – Por que ao cortar um array fora do intervalo ele não está devolvendo ‘nil’?

A documentação ruby fala que ao tentar “cortar” um array usando um índice inválido ele irá devolver nil, então porque eu recebo sempre um array vázio no índice após o último índice válido?

array = [0, 1, 2]

# retornando um array vazio como esperado
p array[2,0] # []

# o índice 3 não existe então por que ele retorna '[]'?
p array[3,0] # []

# fora do intervalo retorne 'nil'
p array[4,0] # nil

Ruby Package – Mind Piff | Forum Promotion

ruby – Rails. Order number creation

I’m having an issue optimizing it properly.

So I’m having an order number generator that looks like this

get_date = Date.today.strftime('%d%m%Y')
last_value = Order.where(created_at: Date.today.all_day).order(:created_at).last&.order_number&.split('-')&.last.to_i + 1
self.order_number ||= "#{get_date}-#{last_value}"

So he always adds + 1 to the latest value which was found from last_value leaning on the date. Might be this part could be adjusted/skipped somehow :).

Would like to hear your advice as it seems dirty to me.

Ruby Package – VGR | Forum Promotion