Asked  7 Months ago    Answers:  5   Viewed   25 times

I have a variable var = "some_name" and I would like to create a new object and assign it to some_name. How can I do it? E.g.

var = "some_name"
some_name = Struct.new(:name) # I need this
a = some_name.new('blah') # so that I can do this.

 Answers

34

You cannot dynamically create local variables in Ruby 1.9+ (you could in Ruby 1.8 via eval):

eval 'foo = "bar"'
foo  # NameError: undefined local variable or method `foo' for main:Object

They can be used within the eval-ed code itself, though:

eval 'foo = "bar"; foo + "baz"'
#=> "barbaz"

Ruby 2.1 added local_variable_set, but that cannot create new local variables either:

binding.local_variable_set :foo, 'bar'
foo # NameError: undefined local variable or method `foo' for main:Object

This behavior cannot be changed without modifying Ruby itself. The alternative is to instead consider storing your data within another data structure, e.g. a Hash, instead of many local variables:

hash = {}
hash[:my_var] = :foo

Note that both eval and local_variable_set do allow reassigning an existing local variable:

foo = nil
eval 'foo = "bar"'
foo  #=> "bar"
binding.local_variable_set :foo, 'baz'
foo  #=> "baz"
Tuesday, June 1, 2021
 
Kenny
answered 7 Months ago
97
  1. To find out where gems are being installed to, run echo $GEM_HOME in a terminal.
  2. When using RVM, gems are installed into your RVM install as it changes $GEM_HOME. Running echo $GEM_HOME now would show a path into your RVM install.
  3. When Bundler is added to the mix, gems will either be installed in $GEM_HOME, or, if you specify a path when running bundle install will be installed to that path. To find out where a gem is through Bundler you can use bundle show gemname to get its full path.
Saturday, July 31, 2021
 
shwabob
answered 4 Months ago
100

I had a similar problem, though I had rvm and ruby installed only for one user. For me, the solution was to check that the application files were owned by the same user for which ruby was installed.

http://www.modrails.com/documentation/Users%20guide%20Nginx.html#user_switching

Saturday, August 21, 2021
 
NathanOliver
answered 4 Months ago
30

The "full blown" dynmamic version would be this one (not tested):

DECLARE  
  v_stmt_str       VARCHAR2(200);
  v_cur            INTEGER;
  v_rows_processed INTEGER;

  col_cnt     INTEGER;
  rec_tab     DBMS_SQL.DESC_TAB;
  rec         DBMS_SQL.DESC_REC;

  num_var NUMBER;
  string_var VARCHAR2(4000);
  date_var DATE;
  -- .. some more variables if needed 
BEGIN
  v_cur := DBMS_SQL.OPEN_CURSOR; -- open cursor 
  v_stmt_str := 'SELECT whatever from ....';
  DBMS_SQL.PARSE(v_cur, v_stmt_str, DBMS_SQL.NATIVE); 
  DBMS_SQL.DESCRIBE_COLUMNS(v_cur, col_cnt, rec_tab);  

  FOR c in 1..col_cnt LOOP
     rec := rec_tab(c);
     IF rec.col_type = DBMS_TYPES.TYPECODE_NUMBER THEN
         DBMS_SQL.DEFINE_COLUMN(v_cur, c, num_var); 
     ELSIF rec.col_type = DBMS_TYPES.TYPECODE_VARCHAR2 THEN
         DBMS_SQL.DEFINE_COLUMN(v_cur, c, string_var, rec.col_max_len); 
     ELSIF rec.col_type = DBMS_TYPES.TYPECODE_DATE THEN
         DBMS_SQL.DEFINE_COLUMN(v_cur, c, date_var); 
     -- .. some more data types if needed
     END IF;
  END LOOP;

  -- Execute
  v_rows_processed := DBMS_SQL.EXECUTE(v_cur);  
  LOOP 
    -- Fetch a row 
    IF DBMS_SQL.FETCH_ROWS(v_cur) > 0 THEN 
      FOR c in 1..col_cnt LOOP
        rec := rec_tab(c);
        IF rec.col_type = DBMS_TYPES.TYPECODE_NUMBER THEN
            DBMS_SQL.COLUMN_VALUE(v_cur, c, num_var); 
        ELSIF rec.col_type = DBMS_TYPES.TYPECODE_VARCHAR2 THEN
            DBMS_SQL.COLUMN_VALUE(v_cur, c, string_var); 
        ELSIF rec.col_type = DBMS_TYPES.TYPECODE_DATE THEN
            DBMS_SQL.COLUMN_VALUE(v_cur, c, date_var); 
        -- .. some more data types if needed
        END IF;
      END LOOP;
      -- Process: do something with num_var or string_var or date_var values
    ELSE
      EXIT; 
    END IF; 
  END LOOP; 
  DBMS_SQL.CLOSE_CURSOR(v_cur); -- close cursor
END;
/

But as already stated in comments, check your requirements if you really need it such dynamic - probably not.

Check Oracle documentation Coding Dynamic SQL and DBMS_SQL for other examples. There are various methods providing different "levels" of dynamic.

Friday, September 3, 2021
 
TheCarver
answered 3 Months ago
20

Using a thread local variable in your struct can be done by placing it in an impl block:

use std::cell::RefCell;

struct Foo;
impl Foo {
    thread_local! {
        // Could add pub to make it public to whatever Foo already is public to.
        static FOO: RefCell<usize> = RefCell::new(0);
    }
}

And is accessible using Foo::FOO:

Foo::FOO.with(|x| println!("{:?}", x));

Playground
Note, however, that accessing this must be done using Foo:: preceding it, since it's not a field, but instead an associated static.

It could also be done by storing a reference to it:

use std::cell::RefCell;
use std::thread::LocalKey;

thread_local! {
    // Note lack of pub
    static FOO: RefCell<usize> = RefCell::new(0);
}
struct Bar {
    // Visibility here changes what can see `foo`.
    foo: &'static LocalKey<RefCell<usize>>,
    // Rest of your data.
}
impl Bar {
    fn constructor() -> Self {
        Self {
            foo: &FOO,
            // Rest of your data.
        }
    }
}
Saturday, November 13, 2021
 
Silver Light
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share