Noob: Code review my first tutorial homework assignment?

Hello!
Im reading through tutorials to teach myself Ruby. I’m a business
programmer by day, but I want to learn something new instead of the same
old thing I’ve been doing for the last 8 years. I don’t know any Ruby
programmers local to me, so I’d like to ask you to please code review
the first half of my ‘project’. If I am doing something wrong, or could
do it better please remember Im just a ruby-noob so please explain it as
best you can so I can understand why I should do things differently.
Thank you much in advance!

Here is what I was asked to do (note this is only the first half of the
project, but Id like to know Im headed in the right direction before I
start the second half).

==================================================

Name the project as MyOwnRubyApp.

Create a new Ruby class called MyOwnRubyClass.

Define a method called my_own_ruby_method inside of the MyOwnRubyClass

class as following

  • The method receives two parameters called my_parameter and
    your_parameter.
  • The method then prints out current time using Time.now.
  • The method then invokes yield(my_parameter, your parameter,
    current_month).
    o The current_month, is an integer value representing current
    month of the year, for example, for the current month of “June”, the
    current_month should be 6.
    o The current_month value needs to computed in your program. For
    example, if you are running the same program during July, it should be
    set to 7. (In other words, you cannot pass hard-coded value in your
    program)

Create an instance of MyOwnRubyClass. Assign it to a variable called

my_instance.

Here is my code. It runs but I get this error also “…My
Documents/NetBeansProjects/HomeWorkLab1/lib/main.rb:23: warning:
multiple values for a block parameter (2 for 1) from …/My
Documents/NetBeansProjects/HomeWorkLab1/lib/main.rb:17”

==================================================

Create a class called MyOwnRubyClass

class MyOwnRubyClass

define a method called my_own_ruby_method

method receives two parameters called my_parameter and your_paramter

def my_own_ruby_method(*parameters)
# method prints our current time using Time.now
yield(Time.now)

# 1) The method then invokes yield(my_parameter, your parameter,

current_month)
# 2) The current_month, is an integer value representing current
month of the year,
# for example, for the current month of “June”, the current_month
should be 6.
# 3) The current_month value needs to computed in your program.
# For example, if you are running the same program during July,
it should be set to 7.
# (In other words, you cannot pass hard-coded value in your
program)
current_month = Date.today.month
#yield “#{parameters.join(’,’)}”, current_month{|x| puts x} # doesnt
work
yield(parameters, current_month)
end
end

my_instance = MyOwnRubyClass.new

my_instance.my_own_ruby_method(“alpha”, “beta”){|s|puts s}

On Thu, Nov 27, 2008 at 8:46 PM, Brian A.
[email protected] wrote:

yield(parameters, current_month)
A: 2
Q: How many paramaters do you pass into the block

my_instance.my_own_ruby_method(“alpha”, “beta”){|s|puts s}
A: 1
Q: I am sure you know it by now.
Cheers
Robert

Ne baisse jamais la tête, tu ne verrais plus les étoiles.

Robert D. :wink:

Robert D. wrote:

On Thu, Nov 27, 2008 at 8:46 PM, Brian A.
[email protected] wrote:

yield(parameters, current_month)
A: 2
Q: How many paramaters do you pass into the block

my_instance.my_own_ruby_method(“alpha”, “beta”){|s|puts s}
A: 1
Q: I am sure you know it by now.
Cheers
Robert

Ne baisse jamais la t�te, tu ne verrais plus les �toiles.

Robert D. :wink:

Thank you for the help. Does this look better? It runs with no error
and seems to make sense to me… Any input is appreciated:

=================================================================================

Create a class called MyOwnRubyClass

class MyOwnRubyClass

define a method called my_own_ruby_method

method receives two parameters called my_parameter and your_paramter

def my_own_ruby_method(my_parameter, your_parameter, current_month)

# method prints our current time using Time.now
yield(Time.now)

# 1) The method then invokes yield(my_parameter, your parameter, 

current_month)
# 2) The current_month, is an integer value representing current
month of the year,
# for example, for the current month of “June”, the current_month
should be 6.
# 3) The current_month value needs to computed in your program.
# For example, if you are running the same program during July,
it should be set to 7.
# (In other words, you cannot pass hard-coded value in your
program
)
yield(my_parameter, your_parameter, current_month)

end
end

my_instance = MyOwnRubyClass.new

current_month = Date.today.month

my_instance.my_own_ruby_method(“alpha”, “beta”, Date.today.month){|arg1,
arg2, arg3| puts “#{arg1} #{arg2} #{arg3}”}

On Fri, Nov 28, 2008 at 4:29 AM, Brian A.
[email protected] wrote:

Robert D. wrote:
Better, but there is still two yields in your method, one with one
parameter one with three.
If this is really needed I would rather declare the block with
variable arguments look at this
514/15 > irb
irb(main):001:0> def a( *args, &blk)
irb(main):002:1> blk.call( args )
irb(main):003:1> end
=> nil
irb(main):004:0> myblock = proc{ |args| case
irb(main):005:2
when args.empty?
irb(main):006:2> puts “empty”
irb(main):007:2> when args.size==1
irb(main):008:2> puts args.first
irb(main):009:2> else
irb(main):010:2
p args
irb(main):011:2> end
irb(main):012:1> }
=> #Proc:0xb7d2f78c@:4(irb)
irb(main):013:0> a &myblock
empty
=> nil
irb(main):014:0> a 1, &myblock
1
=> nil
irb(main):015:0> a 42, 222, 22, &myblock
[42, 222, 22]
=> nil
irb(main):016:0>


Ne baisse jamais la tête, tu ne verrais plus les étoiles.

Robert D. :wink:

On Thu, Nov 27, 2008 at 11:46 AM, Brian A.
[email protected] wrote:

Hello!
Im reading through tutorials to teach myself Ruby. I’m a business
programmer by day, but I want to learn something new instead of the same
old thing I’ve been doing for the last 8 years.

Also, check out Satish T.'s free ruby class:

martin