How to use classes in shoes?
I am a beginner programmer who has experience with processing. I am currently trying to make an application using Shoes, but I am confused about how objects and classes work.
I understand that the following will do the following in Ruby:
class Post
def self.print_author
puts "The author of all posts is Jimmy"
end
end
Post.print_author
But why isn't the following in the shoes? How can I start it?
class Post
def self.print_author
para "The author of all posts is Jimmy"
end
end
Shoes.app do
Post.print_author
end
source to share
I'm not very familiar with Shoes, but the problem you are probably running into is that you are trying to call a method para
in a class Post
and no such method exists.
When you call Shoes.app do ...
, I suspect that Shoes is changing the current execution context to one that includes these methods. That is, you should expect this to work:
Shoes.app do
para "The author of all posts is Jimmy"
end
This is equivalent to:
Shoes.app do
self.para("The author of all posts is Jimmy")
end
When you call Post.print_author
, it is self
no longer a Shoes object, but rather a Post class. At this point, you have several options:
-
Go to the Shoes instance and call your shoe-specific methods on it. You should probably do it this way when you don't need any state from the Post:
class Post def self.print_author(shoes) shoes.para "The author of all posts is Jimmy" end end Shoes.app do Post.print_author(self) end
-
Create a Post class that accepts a Shoes object so you don't need to skip it. You should do it this way if the Post will have a significant number of states:
class Post def initialize(shoes) @shoes = shoes end def print_author @shoes.para "The author of all posts is Jimmy" end end Shoes.app do post = Post.new(self) post.print_author end
-
You can use the option on option 2. to automatically transfer calls to the object
@shoes
. This is starting to creep into Ruby metaprogramming, which I would recommend that you avoid until you are more comfortable with Ruby, but I leave it here to keep you interested:class Post def initialize(shoes) @shoes = shoes end def print_author para "The author of all posts is Jimmy" end def method_missing(method, *args, &block) @shoes.send(method, *args, &block) end end Shoes.app do post = Post.new(self) post.print_author end
What it means is Ruby says "if the method is not found on the Post instance, try submitting it instead of the @shoes instance." As you can imagine, some very nice DSLs can afford this, but you must use them carefully as it can make your code difficult to execute if you overuse it.
source to share
An easier way to do this is to Post
provide content and then display that content in your Shoes application, but you want it. Side benefit: you can reuse the Post class in another class that prints to the console.
class Post
def self.print_author
"The author of all posts is Jimmy"
end
end
Shoes.app do
para Post.print_author
end
class ConsoleApp
def run
puts Post.print_author
end
end
source to share