Ruby/tk Help Please

I am a complete beginner when it comes to programming and i need some
help.
I don’t know very much computer language jargan so please layman terms
as much as possible.
I began trying to learn to program about a year ago when I took my first
programming class:
1st language => Python => understood
2nd language => Java => no understanding whatsoever
3rd language => Ruby/tk => currently trying to learn, some basics
understood, but i don’t understand how to use blocks and several other
huge concepts.

OBJECTIVE: create a GUI that stores browsing history in the form of Host
name, IP address, and whether nearby hosts exists (Boolean + count).
Also, the user should be able to search the storage place by either host
name or IP address to organize results.
I am currently using Unix KDevelopRuby and i am running the program via
the command line (ruby gui.rb) and i so far have:

#-------------------program
begin------------------------------------------------

#!/usr/bin/env ruby

require ‘tk’

class HistBox

inst_section = {

list1 = [“All Categories”,“Host Name”, “IP Address”, “Nearby Hosts”,
“Other Data of Interest”]

list2 = [10,20,30,40,50,60,70,80]

list3 = [10,20,30,40,50,60,70,80]

@var = TkVariable.new

@list2 = list2

root = TkRoot.new do

            title "History"

end

---------------Frame----------------------

entry_frame = TkFrame.new(root) do

            pack 'side' => 'top'

end

#----------Entry and DropDownMenu--------------

entry1 = TkEntry.new(entry_frame) do

            pack 'side' => 'left'

end

dropMenu = TkOptionMenubutton.new(root,@var,*list3) do

            pack 'side'=>'top'

end

#--------------Buttons----------------------------

button1 = TkButton.new(root) do

            text "Search"

            pack 'fill'=>'both', 'side' => 'top'

end

button2 = TkButton.new(root) do

            text "Exit"

            pack 'fill'=>'both', 'side' =>'top'

end

label = TkLabel.new(root) do

            text "Search results: "

            pack 'side' => 'left'

end

button1.command proc {

            result = entry1.value.to_i * dropMenu.value.to_i

            label.text = "The result is: #{result}"

}

button2.command { exit }

end # -------------end class---------------------

HistBox.new

Tk.mainloop

#----------------------------------------program
end--------------------------------------

as of now the program simply adds the entered value in entry1 to the
value selected from the dropdownmenu and displays the result below
following the “search results:” label.

however, i am now stuck, i would like the program to be able to talk to
the hosts visited by the user and store this info somewhere so that it
can be searched by this gui by either all categories or a specified
category (change pull down menu to reflect list1) .

Also, i would like to produce the results in some sort frame following
the Search results label, and i think it would be helpful to display the
results in treeView unless anyone can think of a better idea.

TreeView example:

                   ___________________________

Search results::expressionless: + www.google.com |

                   | + www.yahoo.com                  |

                   |  - www.aol.com                       |

                   |       - Host Name                      |

                   |              - AOL                         |

                   |       - IP Address                      |

                   |              - 999.999.9.9              |

                   |       -Other Args                       |

                   |               - info                         |

                   |___________________________|

So, yeah i hope that gives you guys a pretty good idea of what i’m
trying to do so hopefully you might be able to help me. Any advice is
very much appriciated as i continue to guess and check my work :-/.
thank you for your time.

Sean Ob wrote:

I am a complete beginner when it comes to programming and i need some
help.
I don’t know very much computer language jargan so please layman terms
as much as possible.
I began trying to learn to program about a year ago when I took my first
programming class:
1st language => Python => understood
2nd language => Java => no understanding whatsoever

Well, there’s your first problem. :slight_smile: Java isn’t a very good language,
but it works on a somewhat different (and more common?) paradigm than
Python and Ruby. It’s to your advantage as a programmer to understand
as many paradigms as possible.

3rd language => Ruby/tk => currently trying to learn, some basics
understood, but i don’t understand how to use blocks and several other
huge concepts.

Your second and third problems, then. You can’t use Ruby profitably
without understanding blocks (have you read Programming Ruby?), and from
what I understand, Ruby/Tk is a really bad GUI library.

Anyway, you shouldn’t even be thinking about GUI development until you
understand how to use the language. GUI libraries are, by nature,
complex things, and so you need to understand how to work with the
language to use them effectively.

OBJECTIVE: create a GUI that stores browsing history in the form of Host
name, IP address, and whether nearby hosts exists (Boolean + count).
Also, the user should be able to search the storage place by either host
name or IP address to organize results.

Is this a homework problem?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Sean Ob wrote:

I am a complete beginner when it comes to programming and i need some
help.
I don’t know very much computer language jargan so please layman terms
as much as possible.
I began trying to learn to program about a year ago when I took my first
programming class:
1st language => Python => understood
2nd language => Java => no understanding whatsoever

Well, there’s your first problem. :slight_smile: Java isn’t a very good language,
but it works on a somewhat different (and more common?) paradigm than
Python and Ruby. It’s to your advantage as a programmer to understand
as many paradigms as possible.

i have read a lot of things pertaining to blocks, but i still don’t
understand them.

for example i have tried several variations of

result*@list2.each + ‘\n’
#i realize this is horrible syntax, but what i want to do is take result
and then print the result*each element of list2 on separate lines
This was one of the first bumps i hit then after giving up on figuring
that out i figured it would be much easier to build a TreeView object
and just print that but now i am struggling to construct the elements of
the tree.
i realize my method of just jumping into a language trying to do complex
things right away is not a very logical approach however i learn best by
doing and by example. I give myself an objective and try to figure out
how to get there, it’s just how i learn best. I Have a Masters in
Economics, programming is a new hobby i have picked up, in hopes that
eventually i will compile enough knowledge to be able to program more
efficiently. however, for now i’m just shooting for functionality.

result*@list2.each + ‘\n’ do {|x| puts x}# forgot the block

No, not homework, i’ve graduated, i’m just trying to learn more about
web development and how to actually get programs i write to interact
with real world applications. I have given up on java and I don’t
really like Python too much, so i am under the understanding that ruby
is the new upcoming web developer language so i’m trying to learn about
it and how to use it. i’m simply seeking tips and advice to assist my
learning because i cant seem to find very useful information online for
someone with as low of a level of expertise as i have.
More specifically i’m looking for help in developing (and explaining
fuctionality)of classes and methods to compute the following:

  • store web history info somewhere to be used and printed in the gui
  • search the object that contains the history info
  • display the info in an organized manor in the gui
    I am stuck and i have no idea how i would even begin to execute these
    tasks.
    Thank You, Hope to hear from someone soon.

Sean Ob wrote:

result*@list2.each + ‘\n’
#i realize this is horrible syntax, […] however, for now i’m just shooting for functionality.

Well, Ruby programmers (Rubyists) enjoy beautiful code. It’s rather like
poetry.
There’s a few choices. Why is @list2 an instance variable, first of all?
1)
@list2.each { |element| puts element * result }

require ‘pp’
@list2.map! { |element| element * result }

More dangerous, as each element of the list changes.

pp @list2

The code ought to be elegant. Do take the time to learn the syntax, the
“Ruby way”. You will thank yourself later on.
If you do not wish to learn the syntax, then you should go to
www.gigamonkeys.com and study LISP for about a month, then come back.

Sean Ob wrote:
[…]

i have read a lot of things pertaining to blocks, but i still don’t
understand them.

Then keep reading and practicing. There is no shortcut.

for example i have tried several variations of

result*@list2.each + ‘\n’
#i realize this is horrible syntax, but what i want to do is take result
and then print the result*each element of list2 on separate lines

It’s incorrect syntax. Think of it this way:

In English, you’re saying “I want to do something with each element of
@list2.”
In Ruby, that’s
@list2.each do |element|
something
end

The each method takes each element in turn and calls the block with that
element.

Now, what’s the “something” you want to do with each element? You want
to multiply each element by result, convert the result to a string, and
append \n. In Ruby, that’s
(element * result).to_s + “\n”

Putting those constructs together, we get
@list2.each do |element|
(element * result).to_s + “\n”
end

Does that make more sense?

In fact, I probably wouldn’t write it like this. I’d probably do
@list2.collect {|e| e * result}.join “\n”

The output is slightly different, but it’s probably what you want.

Determining why this works (and the difference in output) is left as an
exercise to the student. :slight_smile:

[…]

i realize my method of just jumping into a language trying to do complex
things right away is not a very logical approach however i learn best by
doing and by example.

So do I. Just make sure you learn your prerequisites.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sean Ob wrote:

No, not homework, i’ve graduated, i’m just trying to learn more about
web development

Then you’re barking up the wrong tree with Ruby/Tk. That’s meant for
desktop app development. Web development is totally different (and
easier!), and Tk has nothing to do with it; rather, you’ll want to use a
Web framework. Rails is by far the most popular Ruby Web framework,
although there are others; you could even get along without a framework,
although I don’t recommend it.

and how to actually get programs i write to interact
with real world applications.

What do you mean?

I have given up on java and I don’t
really like Python too much, so i am under the understanding that ruby
is the new upcoming web developer language so i’m trying to learn about
it and how to use it.

Well, first of all, understand that Web development and desktop app
development are not that closely related, and different tools are
usually necessary.

i’m simply seeking tips and advice to assist my
learning because i cant seem to find very useful information online for
someone with as low of a level of expertise as i have.
More specifically i’m looking for help in developing (and explaining
fuctionality)of classes and methods to compute the following:

  • store web history info somewhere to be used and printed in the gui
  • search the object that contains the history info
  • display the info in an organized manor in the gui
    I am stuck and i have no idea how i would even begin to execute these
    tasks.

Then you’re really not ready to think about them yet. Read Programming
Ruby. Read the Rails guides if you’re planning to use Rails. Try
something simple first. The pieces will fall into place as you actually
create stuff.

Bonus tip: do all development test-first (investigate RSpec and
Cucumber) – no exceptions. It feels slower at first, but it will save
you a lot of trouble.

Thank You, Hope to hear from someone soon.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Sean Ob wrote:

No, not homework, i’ve graduated, i’m just trying to learn more about
web development

Then you’re barking up the wrong tree with Ruby/Tk. That’s meant for
desktop app development. Web development is totally different (and
easier!), and Tk has nothing to do with it; rather, you’ll want to use a
Web framework. Rails is by far the most popular Ruby Web framework,
although there are others; you could even get along without a framework,
although I don’t recommend it.

Sorry, i meant software development, my mind is focused on web right now
because i’m trying to find a list of ruby methods that allow the program
to interact with a web browser.

and how to actually get programs i write to interact
with real world applications.

What do you mean?

when i was in school i learned about a lot of different code and what
the code could do however it was things like, create a hash table, write
a program to compute square root, write a size method, write a queue
method, etc…
all of the building blocks yes, but never anything on how to put this
knowledge to use in a real program that actually does something outside
of the terminal.
this is my first time actually working with a gui.

Then you’re really not ready to think about them yet. Read Programming
Ruby. Read the Rails guides if you’re planning to use Rails. Try
something simple first. The pieces will fall into place as you actually
create stuff.

Do you mean the ‘pickaxe’ book? someone else told me that so i checked
it out from the library and it had a lot of information but it also had
a lot of bad (in my unexperienced opinion) explainations for how and why
the code returned what it did

it gives you a simple example such as

def recur(a,b)
________a=c
________b.times(c = yield a)
________c
end
recur(2,4){|x| puts x}

but then doesn’t explain in much detail what is going on before jumping
into a super complex example with even less explanation. Also, there is
nothing about tk in the book, which is what i’m mostly interested in
because i think creating gui’s would be pretty cool if i could get good
at it.

Bonus tip: do all development test-first (investigate RSpec and
Cucumber) – no exceptions. It feels slower at first, but it will save
you a lot of trouble.

I don’t know what you mean by this?

Marnen Laibow-Koser wrote:

[…]

i realize my method of just jumping into a language trying to do complex
things right away is not a very logical approach however i learn best by
doing and by example.

So do I. Just make sure you learn your prerequisites.

I started the path on Ruby largely with projecteuler.net and Why the
Lucky Stiff’s guidebook (“Chunky Bacon!”).
And I turned out fine.


Stop laughing.

okay, so back to one of my other questions, what methods can i use (with
what parameters/args) to get my program to collect and store browsing
history?
also, in what form would be the best way to store it?
would it be best to create a separate program to do this or can i just
put it in with my tk stuff?
Do i need to require anything in order to collect this information?
for the browsing history i would like to collect the host name and ip
address of the sites visited and simply have the ability to search for
them by name and print.

Seeing as though i am really struggling with this, i may take a step
backward and change my objective to creating a more simple search-able
database of contact info before i begin to understand more about how to
make a program interact with a web browser … I’ll let you know if i
give-up.

On a better note, one week into learning ruby and i have a fully
functional multiplier-GUI… slowly but surely getting a grip

In fact, I probably wouldn’t write it like this. I’d probably do
@list2.collect {|e| e * result}.join “\n”
does this mean “take each element of @list2 and multiply it by result,
then print the collection and insert a new line between each element but
not on either end”
or does it include the ends?
or am i way off?
…experimenting …

You are not going to need the join method. The puts method
automatically
creates a new line.

@list2.each { | e | puts e*result}

i have read a lot of things pertaining to blocks, but i still don’t

understand them.

Then keep reading and practicing. There is no shortcut.

for example i have tried several variations of

result*@list2.each + ‘\n’
#i realize this is horrible syntax, but what i want to do is take result
and then print the result*each element of list2 on separate lines

It’s incorrect syntax. Think of it this way:

In English, you’re saying “I want to do something with each element of
@list2.”
In Ruby, that’s
@list2.each do |element|
something
end

The each method takes each element in turn and calls the block with
that
element.

Now, what’s the “something” you want to do with each element? You want
to multiply each element by result, convert the result to a string, and
append \n. In Ruby, that’s
(element * result).to_s + “\n”

Putting those constructs together, we get
@list2.each do |element|
(element * result).to_s + “\n”
end

Does that make more sense?

In fact, I probably wouldn’t write it like this. I’d probably do
@list2.collect {|e| e * result}.join “\n”

The output is slightly different, but it’s probably what you want.

Determining why this works (and the difference in output) is left as an
exercise to the student. :slight_smile:

[…]

i realize my method of just jumping into a language trying to do complex
things right away is not a very logical approach however i learn best by
doing and by example.

So do I. Just make sure you learn your prerequisites.

There are probably people reading this and laughing at my lack of
programming knowledge, but this here is a perfect example of the type of
explanations i am looking for. Thank you, i very much appreciate your
ability to communicate these concepts to me.

Sean Ob wrote:

In fact, I probably wouldn’t write it like this. I’d probably do
@list2.collect {|e| e * result}.join “\n”

Please learn to quote properly when replying.

does this mean “take each element of @list2 and multiply it by result,
then print the collection and insert a new line between each element but
not on either end”
or does it include the ends?
or am i way off?

Why don’t you read the docs and find out?

…experimenting …

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Also, there is
nothing about tk in the book, which is what i’m mostly interested in
because i think creating gui’s would be pretty cool if i could get good
at it.

In that case, I’d highly recommend you have a look at the
http://www.tkdocs.com site that I put together.

It provides a broad tutorial about how to use Tk, and includes lots of
explanation of the concepts behind the different features you’ll be
using. There are also lots of examples, both small ones and larger ones
that are built up one piece at a time.

I’ve gotten a lot of positive feedback from people that it helped them
get going with GUI programming, so hopefully it might be of help to you.

Mark

On Thu, Nov 19, 2009 at 12:48 AM, Sean Ob [email protected] wrote:

i realize my method of just jumping into a language trying to do complex
things right away is not a very logical approach however i learn best by
doing and by example. I give myself an objective and try to figure out
how to get there, it’s just how i learn best. I Have a Masters in

I suggest you take a couple of days first and work through Chris
Pine’s tutorial [Learn to Program, by Chris Pine]. It’s a “learn by
doing” tutorial, so it should fit your preferred learning style, and
everything you learn there will be directly applicable to your problem

  • it’s all the basic building blocks of the language.

martin

Marnen Laibow-Koser wrote:

Anyway, don’t get stuck on Tk. The survey results at
The 2008 Ruby GUI Survey make pretty clear that it’s the
worst possible option for Ruby GUIs. Read the report and choose
something else.

That is true. If you want to get off the ground quickly, you’re probably
better off using Shoes.

Aldric G. wrote:

Marnen Laibow-Koser wrote:

Anyway, don’t get stuck on Tk. The survey results at
The 2008 Ruby GUI Survey make pretty clear that it’s the
worst possible option for Ruby GUIs. Read the report and choose
something else.

That is true. If you want to get off the ground quickly, you’re probably
better off using Shoes.

Or maybe Limelight.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sean Ob wrote:
[…]

Read Programming
Ruby. Read the Rails guides if you’re planning to use Rails. Try
something simple first. The pieces will fall into place as you actually
create stuff.

Do you mean the ‘pickaxe’ book?

Yes.

someone else told me that so i checked
it out from the library and it had a lot of information but it also had
a lot of bad (in my unexperienced opinion) explainations for how and why
the code returned what it did

it gives you a simple example such as

def recur(a,b)
________a=c
________b.times(c = yield a)
________c
end
recur(2,4){|x| puts x}

but then doesn’t explain in much detail what is going on before jumping
into a super complex example with even less explanation.

How much detail do you want? Although Programming Ruby is good for
learning the language, it is definitely a reference book, not a
textbook, so there’s not a lot of handholding and you’re expected to get
a lot out of fairly terse explanations. There are more beginner-level
textbooks for Ruby out there – unfortunately, I’m not sure which ones
to recommend.

Also, there is
nothing about tk in the book, which is what i’m mostly interested in
because i think creating gui’s would be pretty cool if i could get good
at it.

There is a section about Tk in the book, but so what? You can’t expect
the Pickaxe Book to document every library you want to use.

Anyway, don’t get stuck on Tk. The survey results at
The 2008 Ruby GUI Survey make pretty clear that it’s the
worst possible option for Ruby GUIs. Read the report and choose
something else.

Bonus tip: do all development test-first (investigate RSpec and
Cucumber) – no exceptions. It feels slower at first, but it will save
you a lot of trouble.

I don’t know what you mean by this?

Did you try Google yet?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]