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?
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?
Example:
foo = Object.new
def foo.bar
s = "Hello World"
y = "There"
end
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.
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.
The object you show is a hash in Ruby except you:
"s":
."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"}
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.
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
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