Why are my hashes printed as strings? - ruby

It's pretty wierd, but I do not know what to configure or where to configure. I am trying to print a simple hash value as below:
#!/usr/bin/ruby
names = Hash.new
names[1] = "Jane"
names[2] = "Thomas"
puts names
I expect the output to be
{1=>"Jane", 2=>"Thomas"}
while I get
1Jane2Thomas
Any ideas?

You should use inspect.
puts names.inspect
#=> {1=>"Jane", 2=>"Thomas"}

The puts method calls to_s on its argument(s) and prints the result. The p method however calls inspect on its argument(s) and prints the result:
{1=>"Jane", 2=>"Thomas"}.to_s
#=> '1Jane2Thomas'
{1=>"Jane", 2=>"Thomas"}.inspect
#=> '{1=>"Jane", 2=>"Thomas"}'
So, to have a nice Hash printout, either use
puts {1=>"Jane", 2=>"Thomas"}.inspect
or
p {1=>"Jane", 2=>"Thomas"}

Related

Join a ruby enumerator into a string

I have an instance of Enumerator::Generator that yields strings. I need to join them into a single string.
What's a good way of doing this? I notice that * doesn't work. I know I can .map {|x| x} first but that seems rather un-idiomatic
I think in this case I' might reach for inject/reduce (aliases for the same method, reduce as a name makes more sense, to me) with the + operator:
enum.reduce(:+)
# or, passing in a block
enum.reduce(&:+)
As a full example:
# never used Enumerator::Generator directly, but you called it out specifically
# in your question, and this seems to be doing the trick to get it working
enum = Enumerator::Generator.new do |y|
y.yield "ant"
y.yield "bear"
y.yield "cat"
end
p enum.reduce(&:+) # output: "antbearcat"
# crude example of modifying the strings as you join them
p enum.reduce('') { |memo, word| memo += word.upcase + ' ' }
# output: "ANT BEAR CAT "
a=["Raja","gopalan"].to_enum #let's assume this is your enumerator
Write the following code
p a.map(&:itself).join
Or
p a.to_a.join
output
"Rajagopalan"

Ruby puts command in modules returning nil

i am working though LearnTocodethehardway.com http://ruby.learncodethehardway.org/book/ex25.html
On ex25. In the example there is a module that has a bunch of methods that return or print values. The Print_last_word method when supplied with an array of strings just puts nil. it does this even in his example output. My question would then be why?
To be precise, it doesn't puts nil - it puts the last word and returns nil. Here's the example output:
>> Ex25.print_last_word(words)
wait. # <- this is the output
=> nil # <- this is the return value
puts always returns nil.
UPDATE
There seems to be a bug in print_first_word:
module Ex25
def Ex25.print_first_word(words)
word = words.pop(0)
puts word
end
end
Ex25.print_first_word(["foo", "bar", "baz"])
#=> nil
This is because ["foo", "bar", "baz"].pop(0) returns an empty array and puts [] just returns nil without printing anything.
A working implementation (in the exercise's style) could look like this:
module Ex25
def Ex25.print_first_word(words)
word = words.shift
puts word
end
end
To make it more easy to understand:
"puts" never is used for its return value. It is used for its side effect if you wanted a method that would return a word, you would then do something with that word.
words = ["apple", "cucumber", "apple"]
def give_me_first_word(array_of_words)
array_of_words.first
end
variable_to_be_used_later = give_me_first_word(words)
This function would return "apple"(and in this case the variable would be assigned "apple"), but it would puts nothing to the screen. This value would then be used in another program or function.
If you puts a value, you would then not need to return it to any other program as it's already served its purpose. It's actually intuitive because if puts also returned the value, it would be doing two things. The counterpart to "puts" would be "return" that simply returns the value and does not display it to the screen.

odd usage of "end" in Sample code

Looking through this I notice something I have never seen before on line 83.end.map(&:chomp) so end is an object? (I realize that might be 100% wrong.) Can someone explain what and how that works there? What exactly is advantage?
No, end is not an object, but object.some_method do ... end is an object (or rather it's evaluated to an object) - namely the object returned by the some_method method.
So if you do object.some_method do ... end.some_other_method, you're calling some_other_method on the object returned by some_method.
The full code snippet you're referring to is below:
def initialize(dict_file)
#dict_arr = File.readlines(dict_file).select do |word|
!word.include?("-") && !word.include?("'")
end.map(&:chomp)
end
notice that the end you're talking about is the end of the block that starts on the 2nd line (it matches the do on line 2).
Perhaps if you see it parenthesized, and rewritten with curly braces, it will make more sense:
def initialize(dict_file)
#dict_arr = (File.readlines(dict_file).select { |word|
!word.include?("-") && !word.include?("'")
}).map(&:chomp)
end
It's often helpful to examine what Ruby is doing, step-by-step. Let's see what's going with the method ComputerPlayer#initialize:
def initialize(dict_file)
#dict_arr = File.readlines(dict_file).select do |word|
!word.include?("-") && !word.include?("'")
end.map(&:chomp)
end
First, create a file:
File.write("my_file", "cat\ndog's\n")
When we execute:
ComputerPlayer.new("my_file")
the class method IO#readlines is sent to File, which returns an array a:
a = File.readlines("my_file")
#=> ["cat\n", "dog's\n"]
Enumerable#select is sent to the array a to create an enumerator:
b = a.select
#=> #<Enumerator: ["cat\n", "dog's\n"]:select>
We can convert this enumerator to an array to see what it will pass to it's block:
b.to_a
=> ["cat\n", "dog's\n"]
The enumerator is invoked by sending it the method each with a block, and it returns an array c:
c = b.each { |word| !word.include?("-") && !word.include?("'") }
#=> ["cat\n"]
Lastly, we send Enumerable#map with argument &:chomp (the method String#chomp converted to a proc) to the array c:
c.map(&:chomp)
#=> ["cat"]
A final point: you can improve clarity by minimizing the use of !. For example, instead of
...select do |word|
!word.include?("-") && !word.include?("'")
consider
...reject do |word|
word.include?("-") || word.include?("'")
You might also use a regex.

Ruby: how does one print a 2D array?

puts WINNING_ROWS.each{ |solution| "[ #{solution.map{ |space| "#{space}"}} ]"}
I tried doing the above, but it just lists each value with a new line char afterwards.
I'm trying to get the output:
[stuff,in,row,1]
[stuff,in,row,2]
etc
If this is just for debugging, the usual way is to say either
p expression
or
puts expression.inspect
...which is about the same thing.
You can also use pp.
require 'pp'
pp expression
pp(expr)
One could do something like this:
WINNING_ROWS = [[1,2,3],[4,5,6]]
WINNING_ROWS.map { |x| x.inspect }.join("")
Which will get you a string formatted as you requested
You're relying on the default to_s of Array. #each returns the array itself. So what you're doing is the same as puts WINNING_ROWS. Also, keep in mind that puts adds a newline at the end, so if you don't want that you have to use write (which is not available in the Kernel module like puts is, so you must call it directly on your STDOUT output).
You probably want something like:
WINNING_ROWS = [[1,2,3],[4,5,6]]
WINNING_ROWS.each {|row| STDOUT.write row.inspect }
=> [1, 2, 3][4, 5, 6]
# or this may work for you as well
# STDOUT.write WINNING_ROWS.inspect

Why do this Ruby object have both to_s and inspect methods that appear to do the same thing?

Why do this Ruby object both a to_s and inspect methods that appear to do the same thing?
The p method calls inspect and puts/print calls to_s for representing the object.
If I run
class Graph
def initialize
#nodeArray = Array.new
#wireArray = Array.new
end
def to_s # called with print / puts
"Graph : #{#nodeArray.size}"
end
def inspect # called with p
"G"
end
end
if __FILE__ == $0
gr = Graph.new
p gr
print gr
puts gr
end
I get
G
Graph : 0
Graph : 0
Then, why does Ruby have two functions do the same thing? What is the difference between to_s and inspect?
And what's the difference between puts, print, and p?
If I comment out the to_s or inspect function, I get as follows.
#<Graph:0x100124b88>
#<Graph:0x100124b88>
inspect is used more for debugging and to_s for end-user or display purposes.
For example, [1,2,3].to_s and [1,2,3].inspect produce different output.
inspect is a method that, by default, tells you the class name, the instance's object_id, and lists off the instance's instance variables.
print and puts are used, as you already know, to put the value of the object's to_s method to STDOUT. As indicated by Ruby's documentation, Object#to_s returns a string representing the object -- used for end-user readability.
print and puts are identical to each other except for puts automatically appends a newline, while print does not.
To compare with Python, to_s is like __str__ and inspect is like __repr__. to_s gives you a string, whereas inspect gives you the string representation of the object. You can use the latter to construct an object if you wish.
Further, there is a to_str method on certain objects, which you would call when you need a String-like object, and not just a string representation. (Try in IRB: [1,2,3].to_str and it will fail, yet [1,2,3].to_s will not.) I feel I should mention this because I've been bitten by it before :)
For anyone arriving here after starting out with Ruby Koans, a simple example of where to_s and inspect differ in output is this:
nil.to_s # will yield an empty string, ie ""
nil.inspect # will yield the string "nil"
puts generally prints the result of applying to_s on an object, while p prints the result of inspecting the object.
There is a subtle difference between inspect and to_s:
inspect, when applied on an object, returns the object hex code
along with the instance variable
to_s, when applied on an object,returns only the object hex code
class Item
def initialize(abc)
#abc=abc
end
end
x= Item.new(22)
puts x #prints object x hex code
puts x.inspect #prints object x hex code WITH INSTANCE VARIABLE #abc
puts x.to_s #prints object x hex code
p x #prints object x hex code WITH INSTANCE VARIABLE #abc
p x.inspect #prints object x hex code WITH INSTANCE VARIABLE #abc
p x.to_s #prints object x hex code
Answer from Chris Pine's Learn To Program book
"The inspect method is a lot like to_s, except that the string it returns tries to show you the ruby code for building the object you passed it."
Thus the inspect method will return an array for example like this...
[25, 16, 9, 4, 1, 0]
Where as puts / to_s will return
25
16
9
4
1
0
2.0.0p195 :075 > puts (1..5).to_a # Put an array as a string.
1
2
3
4
5
=> nil
2.0.0p195 :076 > puts (1..5).to_a.inspect # Put a literal array.
[1, 2, 3, 4, 5]
=> nil
2.0.0p195 :077 > puts :name, :name.inspect
name
:name
=> nil
2.0.0p195 :078 > puts "It worked!", "It worked!".inspect
It worked!
"It worked!"
=> nil
2.0.0p195 :079 > p :name # Same as 'puts :name.inspect'
:name
=> :name
From the Rails Tutorial
Refer following link for more information and examples explaining difference between "to_s" and "inspect" as well as difference between "puts" and "p".
https://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/108-displaying-objects

Resources