-1

How do I create an object in ruby within a function such that I have something like:

myobj = {
"s": "Hello World"
"y": "There
}

that is accessible by an erb?

3
  • 8
    this question is very vague.. i'm going to take a stab and say you know nothing about ruby.. try starting out with online tutorials
    – ddavison
    Jun 11, 2013 at 14:18
  • @sircapsalot: I wrote one for him :-)))) What an idiot I am :-)))) Jun 11, 2013 at 17:26
  • @Damascusi: Hey, you, here is a good one. Jun 11, 2013 at 17:28

7 Answers 7

6

Example:

foo = Object.new

def foo.bar
  s = "Hello World"
  y = "There"
end
4
  • 1+1? Where is my strings? That makes no sense...
    – Rolando
    Jun 11, 2013 at 14:13
  • Could you be more explicit? I use Java, and your post does not make sense to me.
    – Rolando
    Jun 11, 2013 at 14:14
  • Since we don't really know what you want, and what really does make sense in your case since your explanation is very sparse, you should either extend your example or adapt the knowledge from this answer to your actual problem.
    – pdu
    Jun 11, 2013 at 14:15
  • I think you have what I want, What is the ".bar" for? I can't access it using just foo? Also, what if I wanted to create a new object named foo, is it just a matter of Object.new? Thanks.
    – Rolando
    Jun 11, 2013 at 14:17
4

If what you want is to encapsulate some data without declaring a whole class, a way to do it is with a Struct

A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.

Myobj = Struct.new(:s, :y)
myobj = Myobj.new
myobj.s = "Hello World"
myobj.y = "There"

Read the docs for more info on this.

3
class MyClass
  def initialize(y)
    @s = 'Hello World'
    @y = y
  end

  def s
    @s
  end

  def y
    @y
  end
end

myobj = MyClass.new 'There'
myobj.s # => "Hello World"
myobj.y # => "There"

You can type this whole thing in irb, or write it into a file and require it.

2

The object you show is a hash in Ruby except you:

  • Can't create a symbol as a hash key using "s":.
  • Forgot the comma between the elements.
  • Didn't close the quote for "There.

Also, myobj is a local variable so perhaps you want an instance variable @myobj?

Here's examples of how to do it correctly:

myobj = {
  "s" => "Hello World",
  "y" => "There"
}
myobj # => {"s"=>"Hello World", "y"=>"There"}

myobj = {
  :s => "Hello World",
  :y => "There"
}
myobj # => {:s=>"Hello World", :y=>"There"}

myobj = {
  s: "Hello World",
  y: "There"
}
myobj # => {:s=>"Hello World", :y=>"There"}

@myobj = {
  "s" => "Hello World",
  "y" => "There"
}
@myobj # => {"s"=>"Hello World", "y"=>"There"}

@myobj = {
  s: "Hello World",
  y: "There"
}
@myobj # => {:s=>"Hello World", :y=>"There"}
0
2

I originally wanted just to reply to your comment, but let me make this full answer. Stating from Gorfi's code:

foo = Object.new
# What's going on here? What have we done? This:
# We took a constant "Object", to which the Object class is assigned.
# Then we sent it message ":new". Method #new is a constructor that
# creates a brand new instance of Object.
# Then we assigned that newly created object to the local variable "foo".

Now we want to define a singleton method on the object assigned to foo. There are multiple ways to do it. But let us first make sure that we know what a singleton method is. Normally, methods are associated with classes. For example, class Dog might have an instance method #bark, which means that all Dog instances know how to #bark. Another example, all the objects know their #object_id:

foo.object_id #=> some number
# Here, we sent a message :object_id to foo, which invoked the appropriate method and
# returned us the object id unique to the object instance foo. All the objects know this
# trick. But not all the objects can respond to the method `#bar`:

foo.bar #=> raises NoMethodError

So what Gorfi did was, that he gave the instance foo a very special ability: to respond to #bar. It's called a singleton method of foo, because ordinary Object instances don't know how to respond to #bar. After we write this:

def foo.bar
  puts "I'm special, a singleton in my own set, I know how to respond to bar."
end

Object foo will respond to bar:

foo.bar #=> see what happens

Continuing the example with dogs, we can define Dog class

class Dog
  def bark
    puts "Bow, wow!"
  end
end

Spot, Rover, Minnie = Dog.new, Dog.new, Dog.new

Spot.bark #=> Bow, wow!
Rover.bark #=> Bow, wow!
Minnie.bark #=> Bow, wow!
# All the Dog instances can bark.

# But only Minnie can sing:
def Minnie.sing
  puts "Bauuuuu, uauuuuu, bauuuuu, uauuuuu, " +
    "bow wow wow wow wow wow wow wauuuuuuuuuuuuuu!"
end

Minnie.sing #=> see what happens
Rover.sing #=> see what happens

Now, finally, let us introduce dog weight. Each dog has its weight, naturally. So we represent it in the Dog class like this.

class Dog # we reopen the Dog class first
  attr_accessor :weight  # we introduce dog weight
end

# and now
Spot.weight = 10
Rover.weight = 20
Minnie.weight = 8

# we now can get each dog's weight:
Spot.weight #=> 10
Minnie.weight #=> 8

The Dogs' weight is stored in the instance variable @weight. So, for example, let's define feeding method for dogs, that will increase their weight by 1:

class Dog # reopen the class
  def feed                 # define weight instance method
    @weight = @weight + 1  # increment weight by 1
  end
end

Spot.feed
Spot.weight #=> 11
Spot.feed
Spot.weight #=> 12

As a homework, define a method that will decrease dogs' weight by one, and make it work.

1

Unlike Javascript you must first create a class. Often you will store it in it's file. Later you instantiate it into an object.

class MyClass
  def initialize(var_s, var_y)
    @var_s = var_s
    @var_y = var_y
  end
end

Then you can instantiate it and display the contents using the p method.

myObj = MyClass.new("Hello Word","There")
p myObj   
0

Like this

class Myobj
  attr_reader :s, :y
  def initialize()
      @s, @y = "Hello World ", "There"
   end
end

ob = Myobj.new
print ob.s, ob.y

gives

Hello World There

Not the answer you're looking for? Browse other questions tagged or ask your own question.