Running ruby code - ruby

I am working on a remote server with Putty. My class looks like:
class Hangman
def initialize
puts "Hello world"
end
end
But when I write in Putty ruby hangman.rb, it skips to the next editor console line without showing anything. Any suggestions?

class Hangman
def initialize
puts "Hello world"
end
end
hangman = Hangman.new
So you forgot to crate an instance of the object you defined. Everytime you call .new method on your object, you get a new instance of it and code inside def initialize is ran.

You define the class (with the constructor) but you don't use it. Create an object:
Hangman.new

Related

Why can't Ruby find a method declared right above?

I have a simple file called helper.rb that looks like this:
module MyHelper
def initialize_helper
puts "Initialized"
end
initialize_helper()
end
And another simple file like this:
require_relative 'helper.rb'
include MyHelper
puts "Done"
But when I run this second file, it results in this error:
helper.rb:6:in `<module:MyHelper>': undefined method `initialize_helper' for MyHelper:Module (NoMethodError)
Why can't Ruby find this initializeHelper method defined directly above where I'm calling it???
Try
def self.initialize_helper
puts "Initialized"
end
Without the self., you're declaring an instance method intended to be called on objects, not the module itself. So, for instance, your original code is intended to be used like
module MyHelper
def initialize_helper
puts "Initialized"
end
end
class Foo
include MyHelper
end
Foo.new.initialize_helper
But if you want to call it on the module, you need to have self. in front of it to make it a method on the module itself.

Ruby class printing name in another class

class NameData
def initialize
#name="Cleetus"
end
class Greetings
def hello()
puts "Hello #{#name}! How wonderful to see you today."
end
end
end
greet=Greetings.new
p greet.hello
Im a little bit of a beginner, but i'm trying to get my name to be used in the Greetings class so the string will print with my name inside. Any ideas?
It's common to put multiple classes inside the same module (sometimes called "namespacing") in Ruby, but it's unusual to have a class inside another class. Even if you do it's still a separate class and does not have access to instance variables in the "outer" class.
What you need to do is to make your #name value accessible outside your NameData object. The usual way to do this in Ruby is with an attribute reader:
class NameData
attr_reader :name
def initialize
#name = "Cleetus"
end
end
name_data = NameData.new
puts name_data.name
# => Cleetus
Once you've done that you need to tell your Greetings object about the NameData object, and how to use it. One way to do that is to pass the NameData object as an argument to hello:
class Greetings
def hello(name_data)
puts "Hello #{name_data.name}! How wonderful to see you today."
end
end
greet = Greetings.new
greet.hello(name_data)
# => Hello Cleetus! How wonderful to see you today.
Another way is to pass it to the Greetings constructor and save it in an instance variable:
class Greetings
def initialize(name_data)
#name_data = name_data
end
def hello
puts "Hello #{#name_data.name}! How wonderful to see you today."
end
end
greet = Greetings.new(name_data)
greet.hello
# => Hello Cleetus! How wonderful to see you today.
The #name variable is an instance variable and it can not be seen by methods within a different class. It can only be seen by methods within the same class. Try this instead:
class Greetings
def initialize
#name="Cleetus"
end
def hello()
puts "Hello #{#name}! How wonderful to see you today."
end
end
greet = Greetings.new.hello

Why does nothing happen when I execute my module in a terminal?

I tried running my code in a Ruby script from my terminal. Nothing happens when I run
ruby Main.rb.
# Main.rb
module Main
class MyClass
def initialize
puts "Hello World"
end
end
end
You need to instantiate your class first, as your puts command will not execute until you invoke MyClass#new. For example:
module Main
class MyClass
def initialize
puts "Hello World"
end
end
end
Main::MyClass.new
Hello World
=> #<Main::MyClass:0x007f9d92144308>
Because it's loading the Module and then doing nothing with it. It never gets instantiated (initialized), only defined.
You define the module like so:
module Main
class MyClass
def initialize
puts "Hello World"
end
end
end
And then initialize it by making a new MyClass object. (On the end of the same file)
test = Main::MyClass.new
Hello World
=> #<Main::MyClass:0x2979b88>
You can handle this in an even better way by only doing this when you run the file directly, not when it's loaded from another ruby file.
if __FILE__ == $0
test = Main::MyClass.new
puts test
end
This way you can do whatever you like when the code is run directly, for example, testing, but just load the module silently every other time.
When running the file directly, it will work as above, but when running this in IRB, you'll only see the following:
=> nil

Unable to Create a Ruby Hash inside a Class

I am trying to create a simple Ruby class but I am getting stuck. This is my code:
#!/usr/bin/ruby
class Dock
#ships = Hash.new(false)
def initialize()
end
def store(ship, pier)
#ships[pier] = ship
end
end
yathi = Dock.new
yathi.store("test", 12)
But when I try to run this by running this in Terminal:
ruby test.rb
This is the error message I am getting:
test.rb:8:in `'store': undefined method `'[]=' for nil:NilClass (NoMethodError)
from test.rb:13
It does work when if I rewrite it like this:
#ships = {pier => ship}
But this creates a new hash everytime with just one value which I don't want. Can someone please tell me what I am doing wrong?
Put the #ships = Hash.new(false) inside the initialize method. When you put it outside the initialize method you are defining a class level instance variable instead. Check out this writeup if you want to know more.
Try initializing the hash inside the "initialize" method so that it is a member of instances of the Dock class instead of a member of the Dock class itself:
class Dock
def initialize()
#ships = Hash.new(false) # <-- Define #ships as an instance variable.
end
def store(ship, pier)
#ships[pier] = ship
end
end

How to call or activate a class?

In my lib folder I have billede.rb:
class Billede
require 'RMagick'
#some code that creates a watermark for a image
image.write(out)
end
How do I call/activate the class? Is the only way to change it to a Rake task?
You can't call a class directly. You have to call a method on that class. For example:
class Billede
def self.foobar
# some kind of code here...
end
end
Then you can call it via Billede.foobar
Perhaps you should read some documentation on basic ruby syntax before trying to do more complex things (such as manipulating images w/ Rmagick).
Code 'inside a class' is run just like any other code. If you have a Ruby file like this:
puts "Hello from #{self}"
class Foo
puts "Hello from #{self}"
end
and you run the file (either via ruby foo.rb on the command line or require "./foo" or load "foo.rb" in a script) it then you will see the output:
Hello from main
Hello from Foo
If you want to load a utility that 'does something' that you can then invoke from a REPL like IRB or the Rails console, then do this:
module MyStuff
def self.do_it
# your code here
end
end
You can require "./mystuff" to load the code, and when you're ready to run it type MyStuff.do_it
And, as you may guess, you can also create methods that accept arguments.
If you want to define a file that can be included in others (with no immediate side effects) but which also "does its thing" whenever the file is run by itself, you can do this:
module MyStuff
def self.run!
# Go
end
end
MyStuff.run! if __FILE__==$0
Now if you require or load this file the run! method won't be invoked, but if you type ruby mystuff.rb from the command line it will.
# in /lib/billede.rb
class Billede
def self.do_something(arg)
# ...
end
def do_anotherthing(arg)
# ...
end
end
# inside a model or controller
require 'billede'
Billede::do_something("arg")
# or
billede_instance = Billede.new
billede_instance.do_anotherthing("arg")

Resources