Why does not work!?


#1



https://www.codecademy.com/courses/ruby-beginner-en-X5wcR/0/8?curriculum_id=5059f8619189a5000201fbcb

(ruby):15: syntax error, unexpected '=', expecting keyword_end
=begin
^
(ruby):25: syntax error, unexpected '=', expecting keyword_end
=end
^
(ruby):53: syntax error, unexpected $end, expecting keyword_end
.......................................................
with the first delete method commes this
..............................
ArgumentError: wrong number of arguments (0 for 1)
from (irb):23:in delete'
from (irb):23:in
delete'
from (irb):47
......................................................................

 class Computer
    @@users = {}
    def initialize(username, password,department)
        @department = department
        @username = username
        @password = password
        @files = {}
        @@users[username] = password
    end
    def create(filename)
        time = Time.now
        @files[filename] = time
        puts "The #{filename} was created in #{time}."
    end
   ### whats wrong here ??!!
   =begin
   
    def delete(filename)
        confirm = Proc.new{|filename| puts "To delete #{filename} for sure press y "
        conf = gets.chomp
        conf == "y"? @files.delete(filename) : puts("Deleting is aborted")
            }
       @files.key?(filename) ? @files.delete(&confirm.call(filename)) : puts("#{filename} does not exist!!") 
    end
    
   =end
    
    ## does not work properly
    def delete(filename)
      conf = ""  
    @files.key?(filename) ? puts("If you sure press y") : puts("#{filename} does not exist!!")
    conf = gets.chomp
    conf == "y" ?  @files.delete(filename) : puts("Deleting #{filename} is aborted")
    @files.key?(filename) ? puts("Deleting is succsessful") : puts("successfully Deleted")
    end
    

    def Computer.get_users
        @@users
    end
end

my_computer = Computer.new("tom",12345,"finance")
my_computer.create("test3")
my_computer.create("test7")
my_computer.create("test4")
my_computer.delete("test3")
res_computer = Computer.new("kim",5666,"resption")
res_computer.create("costumer1")
my_computer.delete("test4")

Computer.get_users


#2

If you remove the =begin and =end lines, that should get rid of

(ruby):15: syntax error, unexpected '=', expecting keyword_end
=begin
^
(ruby):25: syntax error, unexpected '=', expecting keyword_end
=end
^
(ruby):53: syntax error, unexpected $end, expecting keyword_end

I'd recommend not using the ternary operator for if/else unless the logic is very simple or you're tidying things up after you know the code is working. If/else usually allows you to debug code more easily. If you tidied up the delete method a little, you would get something like:

def delete(filename)
  confirm = Proc.new do |filename|
    puts "To delete #{filename} for sure press y "
    conf = gets.chomp

    if conf == "y"
      @files.delete(filename)
    else
      puts("Deleting is aborted")
    end
  end
  

  if @files.key?(filename)
    @files.delete(&confirm.call(filename))
  else
    puts("#{filename} does not exist!!") 
  end
end

You can see a little more clearly now that @files.delete is being called twice - once inside the Proc and once inside the last if statement.

If you fix this, that might help.


#3

thanks verymuch
I tried to get red of it .I liked the ternary opiration:blush: maybe you can take a look

def delete(filename)

    filenum = Proc.new{|x=1| puts "successfully Deleted"; @filesnumder -= x }

    deletproccess = Proc.new{|filename| @files.delete(filename)
    @files.key?(filename) ? puts("Deleting is unsuccsessful") : puts(&filenum.call())
    }    


    deleteactivation = Proc.new{ |filename| puts("If you sure press y") 
    conf = gets.chomp.to_s
    # if deleting is asured start the proccess
    conf == "y" ?  puts(&deletproccess.call()) : puts("Deleting #{filename} is aborted")}
    #puts(&deletproccess.call()) unless conf != "y"}

    ## if the file found go  into deleting proccess
    @files.key?(filename) ? puts(&deleteactivation.call()) : puts("#{filename} doesnt exist!")

end

#4

My recommendation would be to try to get your code working in the simplest possible way first. Try to get the logic working without things like Proc, then it can be a lot more fun to tidy up the code and make it more elegant.


#5

ok :relaxed: got it
thanks


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.