Skip to main content

Creating Incrementing Variables in Code with Ruby

I had a project I was working on that required me to assign values to incremental variables from an array.  A quick solution to the problem, using very little meta-coding is below in its most basic form.  One class was my data or value class.  It held the incremental variables I wanted to assign to.  Here It's called, "Foo".  The class that is using this data is "Bar".  Just for spice, I randomized the array values to assign to the data variables.



class Foo
    attr_accessor :i0, :i1, :i2, :i3

    initialize
        @i0 = 0; @i1 = 0; @i2 = 0; @i3 = 0
    end
end

class Bar
 attr_accessor :f

    def initialize
        @f = Foo.new
        @image_sort = %w{ 1 2 3 4 }.shuffle
    end

    def setup
        @image_sort.each_with_index do  | i, idx |
            @f.instance_variable_set( :"@i#{idx}", i )
        end

        puts @f.i0
        puts @f.i1
        puts @f.i2
        puts @f.i3
    end
end

g = Bar.new
g.setup


Comments

Popular posts from this blog

Codeigniter vs. Kohana Database access speeds

I was doing some basic profiling for a project in which I needed the fastest raw speed I could get with database queries. I'm a fan of Codeigniter for projects that are suitable for it, but had heard from some that Kohana was faster, so I decided to do a very basic comparison of the two. I was using MAMP for OSX, and created a very small db, with a table that had 3 fields: (id), (first), and (last). The data sample was also very small, only a few records. The basic query I tested was a "SELECT * FROM [table]". There is of course nothing remotely scientific about this. It was just a quick ad dirty, very limited comparison. Take it as such. Versions used: CI2.1.3, Kohana 3.3.0. Codeigniter I really like Codeigniter (CI). But, one thing that is very evident from their own profiling functions, is that CI is a bit of a memory hog! Essentially, the same Controller function running in CI takes approximately 10X more memory than in Kohana! This in itself is not ...

Dead Simple React.js with Meteor

I spent a little time exploring the patterns involved in using React.js with Meteor. It's incredibly easy, it turns out. I'll show some examples here. The setup: meteor add kadira:flow-router npm install react react-dom react-mounter npm install react-addons-pure-render-mixin meteor add react-meteor-data Then of course remove all blaze related meteor packages. Ok, Some basic component patterns: Let's create one that accepts a single argument: Hello.jsx import React from 'react'; export const Hello = ({name}) => ( <div>Hello, {name}</div> ); That's all there is to it. Now, let's see a pattern for a component that takes two arguments. We can see that to add further arguments, we can just tack them on after the first two: TwoArgs.jsx import React from 'react'; export const TwoArgs = ({one, two}) => ( <div> <h2>TwoArgs!</h2> <h3>One is: {one}</h3> <h3>Two is: {two}...

Fun with Meteor, React, and React-Bootstrap

React-Bootstrap is pretty cool. I decided to play with it a bit.  Here are the basics. In an already set up Meteor project (set up for React), it is added thus: npm install --save react-bootstrap Once this is done, you also need to add a bootstrap library. It could either be the twitter bootstrap meteor package, or you can link to it. For the purpose of my demo, I just grabbed a couple links from the React-Bootstrap site that they had handy for pulling in from a CMS: index.html <head> <!-- Latest compiled and minified CSS --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css" rel="stylesheet"> </link> <!-- Optional theme --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap-theme.min.css" rel="stylesheet"> </link> </head> Now, let's make a layout, and then create a component tha...