DevHeads.net

Postings by Marc Chanliau

From Functional Style to OO

I'm trying to implement some short functional-style script in an OO
program, and I'm having problems printing the results of the OO version.

Here is the short script:

p = Proc.new { |text| text }
puts p.call( 'Some text gets printed')

=> Some text gets printed

Now the OO version of the above (a bit more involved, but this is just an
exercise for my comprehension):

class Class1
def initialize
@instance = self
@class2 = Class2.new(@instance)
end
def print_text(text)
puts text
end
end

class Class2
def initialize(instance)
@instance = instance
@instance.print_te

curl not found

I have this simple program:

require 'rubygems'
require 'curb'

gem_name = ARGV[0]

raise ArgumentError.new("gem name missing") if gem_name.nil?

if Curl.get("https://rubygems.org/gems/#{gem_name}").status == '200 OK'
$stdout.puts 'Name not available.'
else
$stdout.puts 'Name available.'
end

When I run the program, I get this error:

/Users/marcc/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in
`require': cannot load such file -- curb (LoadError) from
/Users/marcc/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:5

Using a Database without Rails

As a noob I want to play around with databases using Ruby, not Rails. I've
already searched on Google, but I didn't find much help (old, broken links
to gems). I've also checked out stack overflow, but I didn't find what I
was looking for.

I want to write a simple Ruby (not Rails) application that stores user data
in a database and query that database. I've looked into Active Record but
it looks like this is Rails (I want to stay away from Rails for now).

Any pointers?

NewB Question

I'm reading through the Ruby documentation and books. I'm a bit confused
with two symbols (or operators?): double colon (::) and pound sign (#).

I thought double colon '::' was a reference to a constant as in:
Math::PI

But I saw in some doc the following:
"You can create procs in several ways: with Proc::new or by calling either
the Kernel#lambda or Kernel#proc methods."

"Kernel#lambda" and "Kernel#proc" are quite understandable.

Simple Quiz Game

I want to create a simple Quiz program:
1- Display multiple-choice questions one by one (just three in my simple case)
2- Answer each question with your choice letter
3- Display the number of successful answers
4- Display a good-bye message before exiting the program

Unfortunately, my program doesn't get off the ground -- I get the
following error:

/Users/marcc/.rvm/rubies/ruby-2.1.0/bin/ruby -e
$stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)
/Users/marcc/RubymineProjects/Tests/quiz.rb

/Users/marcc/RubymineProjects/Tests/quiz.rb:7:in `display_question':
wrong number of arguments (6

Newbie's need for help

I'm learning to write Ruby using classes (as opposed to scripts).
I can't get the following (extremely simple) class to run (although I
get the corresponding script right). What do I need to do to fix it?

Here is the class:

class Input1

def initialize(n1, n2)
@n1 = n1
@n2 = n2
end

def sum
@n1 + @n2
end

end

puts "Enter first number: "
@n1 = gets
puts "Enter second number: "
@n2 = gets

puts result = Input1(new).sum.to_s

ActiveRecord

I'm learning to use activerecord (not rails).

Two gems are necessary for this: the activerecord gem and the mysql
adpater (I'm using MySQL).

newbie question

This snippet works fine:
def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end

result = fact(4)
puts "The factorial of the number 4 is: " + result.to_s

Now, I want to make the snippet above interactive and allow the user to
enter a number and get in return the factorial for that number. The
following snippet doesn't work, what am I doing wrong?

def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end

puts "enter a positive integer: "
n = gets
puts "factorial: " + fact(n)

Newbie question

In Ruby it seems you can instantiate a class inside the class itself or
outside.