Forum: Ruby Noob: Code review my first tutorial homework assignment?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E2ac3e4c2b6a5ea4fa61bf22e20bfdcf?d=identicon&s=25 Brian A. (judogti)
on 2008-11-27 20:51
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}
==================================================
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-11-27 21:13
(Received via mailing list)
On Thu, Nov 27, 2008 at 8:46 PM, Brian A.
<judobrian+ruby-forum@gmail.com> 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 Dober ;)
E2ac3e4c2b6a5ea4fa61bf22e20bfdcf?d=identicon&s=25 Brian A. (judogti)
on 2008-11-28 04:34
Robert Dober wrote:
> On Thu, Nov 27, 2008 at 8:46 PM, Brian A.
> <judobrian+ruby-forum@gmail.com> 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 Dober ;)

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}"}
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-11-28 12:05
(Received via mailing list)
On Fri, Nov 28, 2008 at 4:29 AM, Brian A.
<judobrian+ruby-forum@gmail.com> wrote:
> Robert Dober 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@(irb):4>
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 Dober ;)
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2008-11-28 22:50
(Received via mailing list)
On Thu, Nov 27, 2008 at 11:46 AM, Brian A.
<judobrian+ruby-forum@gmail.com> 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 Talim's free ruby class:

http://rubylearning.com/

martin
This topic is locked and can not be replied to.