0

I know this code doesn't look good at all , but i just want to explain my requirement. I want to know is there any good or alternative approach to it.

Actually, i want to create a new stack and whenever one stack has reached its capacity. I want to keep track of number of stacks created like @stack_1, @stack_2 ...by incrementing @number += 1 like @stack_@number. And for every stack, i want to maintain a @current_position pointer which is specific to every stack like @stack_2 has @current_position_2. So i want to create dynamic instance variables.

Example:

def initialize
  @number = 1
  @stack+"#{@number}" = Array.new(10)
  @current_position_"#{@number}" = 0
end

Output should be something like @stack1 = Array.new(10).
Lets say if i increment value of @number += 1, it should look like @stack2 = Array.new(10)

2
  • Could you explain what you are trying to achieve? Commented Oct 8, 2014 at 10:25
  • hi @Stefan , added more info to the question on what i want to achieve. Commented Oct 8, 2014 at 16:57

3 Answers 3

2

Instead of array I suggest you to use Hash Map

@stack = Hash.new

@stack[@number] = <Your Array>

Be Careful if the @number is same your array will be replaced..

For more information about hash maps http://www.ruby-doc.org/core-1.9.3/Hash.html

Sign up to request clarification or add additional context in comments.

5 Comments

can you explain why to use hash instead of array ? ..my requirement is to create many stacks. Does hash act like place holder for all stacks ?
@RahulSambari yes.. it will
ill try to fit this suggestion into my scenario and let you know
hey, i updated my question.. lets say i want to maintain a position pointer for every stack..how to go about it
i liked this approach ..but how to maintain @current_position_@number in this scenario ?
1

You can do it like this:

instance_variable_set("@stack#{@number}", Array.new(10, :a))
@stack1
  #=> [:a, :a, :a, :a, :a, :a, :a, :a, :a, :a]

instance_variable_set("@stack#{@number+1}", Array.new(10, :b))
@stack2
  #=> [:b, :b, :b, :b, :b, :b, :b, :b, :b, :b]

instance_variable_set("@current_position_#{@number}", 0)
@current_position_1
  #=> 0

8 Comments

how to achieve for @current_position_"#{@number}" = 0 ? which is not an instance_variable
Does the last addition answer your question?
yep..so can i use both @stack1 and @current_position_1 regularly in my programs ?
Yes, just as though you defined them in the normal way.
how to go in situation like if @current_position_@number < @[email protected]
|
1

Instead of creating instance variables to track a stack's state from the outside, you could create a Stack class that tracks its state internally. Here's a very simple one:

class StackOverflow < StandardError; end

class Stack
  def initialize
    @stack = []
  end

  def position
    @stack.size
  end

  def full?
    position == 2 # small size for demonstration purposes
  end

  def push(obj)
    raise StackOverflow if full?
    @stack << obj
  end
end

stack = Stack.new
stack.push "foo"
stack.full?       #=> false

stack.push "bar"
stack.full?       #=> true

stack.push "baz"  #=> StackOverflow

Having a working stack, you can build something like a StackGroup to handle multiple stacks:

class StackGroup
  attr_reader :stacks

  def initialize
    @stacks = [Stack.new]
  end

  def push(obj)
    @stacks << Stack.new if @stacks.last.full?
    stacks.last.push(obj)
  end
end

stack_group = StackGroup.new
stack_group.push "foo"
stack_group.push "bar"
stack_group.stacks.size #=> 1

stack_group.push "baz"  # no error here
stack_group.stacks.size #=> 2

stack_group.stacks
#=> [#<Stack:0x007f9d8b886b18 @stack=["foo", "bar"]>,
#    #<Stack:0x007f9d8b886a50 @stack=["baz"]>]

2 Comments

i am sorry, but somehow .. i am little confused with your approach. Can you add some comments or is there any easy way to dynamically add numbers to instance variables ? it would be really helpful.
@RahulSambari there's no need for dynamically created instance variables here. stack_groups tracks and crates the stacks and each stack tracks its position. stack_groups.stacks.size returns the total number of stacks, stack_groups.stacks[0].position returns the first stack's position, stack_groups.stacks[1].position returns the second stack's position and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.